Question d’entretien chez NetRex

Write a counter component that increments every second.

Réponse à la question d'entretien

Utilisateur anonyme

17 sept. 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.