useEffect — Synchronising With the Outside World — Concept Check
Questions on useEffect — Synchronising With the Outside World. An effect exists to synchronise your component with something outside React. It is not a lifecycle hook and not a place to put "code that runs after state changes".
Questions in this quiz
8 questions on useEffect — Synchronising With the Outside World — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses `useEffect` to fetch data whenever a user changes a filter. They write: `useEffect(() => { fetchData(filter); }, []);`. What issue might arise from this implementation?
- AThe effect will cause an infinite loop due to missing dependencies.
- BThe effect will not run at all, missing data updates.
- CThe filter value will not update, causing stale data.
- DThe effect will run on every render, causing performance issues.
2.In a React component, an effect is used to set up a WebSocket connection: `useEffect(() => { const ws = new WebSocket(url); return () => ws.close(); }, [url]);`. What is the purpose of the cleanup function here?
- ATo keep the WebSocket open even after the component unmounts.
- BTo ensure the WebSocket is recreated on every render.
- CTo update the URL dynamically without re-rendering.
- DTo prevent memory leaks by closing the WebSocket when the component unmounts.
3.A developer writes `useEffect(() => { document.title = title; }, [title]);`. What is the primary role of this effect?
- ATo synchronize the document title with the component's title state.
- BTo store the title in local storage for future sessions.
- CTo trigger a re-render whenever the title changes.
- DTo directly manipulate the DOM before rendering.
4.Why might using `useEffect(fn)` without a dependency array be problematic?
- AIt prevents the effect from running at all.
- BIt causes the effect to run after every render, which is often unnecessary.
- CIt restricts the effect to run only when specific values change.
- DIt ensures the effect only runs once, missing updates.
5.A developer uses `useEffect` to load data: `useEffect(() => { loadData(); }, [data]);`. Why might this cause an infinite loop?
- AThe effect does not have a cleanup function, causing memory leaks.
- B`data` is likely being recreated each render, causing the effect to rerun.
- CThe dependency array is missing necessary dependencies.
- DThe effect should run before rendering, not after.
6.Which of the following is a legitimate use of `useEffect`?
- AResponding to a user event like a button click.
- BSubscribing to a WebSocket for real-time updates.
- CResetting state when a prop changes.
- DTransforming data for display within the component.
7.A developer mistakenly believes that `useEffect(() => { fetchData(); }, [fetchData]);` will only run once. What is the actual behavior?
- AIt runs on every render if `fetchData` is recreated each time.
- BIt runs only when `fetchData` changes.
- CIt never runs due to missing dependencies.
- DIt runs once on mount and never again.
8.When should `useLayoutEffect` be preferred over `useEffect`?
- AWhen measuring or positioning elements before the user sees them.
- BWhen subscribing to an event source.
- CWhen setting a document title.
- DWhen fetching data from an API.