Pregunta de entrevista de NetRex

Write a counter component that increments every second.

Respuesta de la entrevista

Anónimo

17 sep 2023

Use a setInterval function within the useEffect hook with an empty dependency array. Apply setState callback to make sure that the up to date value of the counter is used. Return clearInterval from the useEffect hook to ensure the interval is removed on component unmount. Use useRef to save component props between renders to avoid stale closures.