NetRex Interview Question

Write a counter component that increments every second.

Interview Answer

Anonymous

Sep 17, 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.