{
  "id": 21069814,
  "title": "You Don't Need That useEffect: Derived State in React",
  "url": "https://urgent.news/2026/11/21/you-dont-need-that-useeffect-derived-state-in-react",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-21T16:10:30.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/mohsenfallahnjd/you-dont-need-that-useeffect-derived-state-in-react-32c8"
  },
  "original_language": "en",
  "account": "The article \"You Don't Need That useEffect: Derived State in React\" discusses the inefficiency of using the useEffect hook to derive state in a React component. It uses a search box over a list example to illustrate the issue. When a user types into the search box, the list updates, but the component renders twice for each keystroke. The initial render shows the old list, and the second render shows the correct list, even though the component's state has changed.\n\nThe problem arises because the filtered results are stored in state, which requires an effect to update whenever the source data (items and query) changes. This creates a lag between the user's action and the component's reaction, known as the \"effect-after-paint gap.\" On slower devices, this can cause a stale frame to be displayed for a brief moment.\n\nThe solution is to compute the filtered results within the component's rendering logic instead of storing them in state. This is done by moving the calculation from within the effect to outside of it. The updated version of the component only renders once, showing the correct list immediately after the user finishes typing.\n\nThe article also mentions that similar patterns can be found in other code snippets, such as computing full names from first and last names or validating cart totals. These calculations should also be performed within the component's render function rather than using effects.\n\nThe recommended approach is to compute values from props or state directly in the component body. When the computation is expensive, useMemo can be used to memoize the result, ensuring it is only recalculated when the input values change. Effects should only be used for interactions with external resources like APIs, subscriptions, timers, or the DOM.\n\nThe author concludes that understanding when to use state versus computed values is crucial for avoiding bugs and improving performance. The key points are:\n\n1. If a value can be calculated from props or state, compute it during rendering instead of storing it in state.\n2. Use useMemo for slow computations to avoid unnecessary re-renders.\n3. Effects are meant for interacting with the outside world, not for copying state values.",
  "summary": "A search box over a list. It works, and it shows up in code review every week: function Results ( { items , query } ) { const [ filtered , setFiltered ] = useState ( items ) ; useEffect ( ( ) => { setFiltered ( items . filter ( ( item ) => item . name . includes ( query ) ) ) ; } , [ items , query ] ) ; return < List items = { filtered } /> ; } Type a letter and the list updates. Nothing looks…",
  "key_points": [
    "Using useEffect to derive state causes inefficiency in React components.",
    "Filtered results stored in state create \"effect-after-paint gap\" on slow devices.",
    "Compute values directly in component rendering to improve performance."
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}