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

156
Views
¿Por qué el acelerador lodash no funciona dentro de los ganchos personalizados useWindowSize?

Estoy tratando de usar el evento de cambio de tamaño con el acelerador. Sin embargo, no funciona. Intenté depurarlo de la siguiente manera:

 import {throttle} from 'lodash' export function useWindowSize() { const [windowSize, setWindowSize] = useState({ width: undefined, height: undefined, }) const handleResize = () => { // handle resize code.... } const onWindowResize = () => { console.log('Throttle') // <-- this does print out throttle(() => { console.log('bam') // <-- this doesn't print out }, 100) } useEventListener('resize', onWindowResize) return windowSize }

Como se ve en el código anterior, he estado tratando de cerrar sesión antes de usar la función de throttle de lodash . Se imprime, pero el registro dentro del throttle no lo hace. ¿Alguien sabe por qué esto tal vez y cómo solucionarlo?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Su función en línea recreada en cada renderizado. Solo asegúrese de que la función de aceleración sea la misma función en el próximo renderizado. Puede usar el enlace useCallback.

 export function useWindowSize() { const [windowSize, setWindowSize] = useState({ width: undefined, height: undefined }); const someFunction = (e) => { console.log("bam", e); // }; const throttleFn = useCallback(throttle(someFunction, 1000), []); const onWindowResize = (e) => { console.log("Throttle", e); throttleFn(e); }; useEventListener("resize", onWindowResize); return windowSize; }
about 4 years ago · Juan Pablo Isaza Report

0

He estado tratando de resolver esto y algo similar, ya que el siguiente código me funciona:

 export function useWindowSize() { const [windowSize, setWindowSize] = useState({ width: undefined, height: undefined, }) const handleResize = useCallback(() => { console.log('handleResize') // It does log this out // handle the resize... }, [windowSize]) useEventListener('resize', throttle(handleResize, 4000)) // call it here instead return windowSize }
about 4 years ago · Juan Pablo Isaza Report
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!