Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

337
Views
¿Por qué aparentemente se captura el valor de cierre de Javascript?
const MyComponent = () => { const history = useHistory(); let [val, setVal] = useState(0); useEffect(() => { trackPageView(); history.listen(trackPageView); }, []); function trackPageView() { console.log('hello: ' + val); setVal(val + 1); } }

useEffect se ejecuta una vez y registra trackPageView con history.listen . A medida que cambia el historial, se llama a trackPageView como se esperaba, pero val siempre tiene el mismo valor inicial de '0'. (El código anterior se basa en el código de este artículo ).

Por el contrario, este código hace algo similar...

 var a = 1; function g(f) { return () => f(); } let func = g(() => console.log('a: ' + a)); func(); a = 2; func();

... pero el a cambiante de a se refleja en la salida. https://jsfiddle.net/s3e250da/

Entonces, ¿qué explica la diferencia en el comportamiento?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!