Urgent.News

What's breaking now, across hundreds of outlets.

Tech

You Don't Need That useEffect: Derived State in React

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…

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.

The 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.

The 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.

The 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.

The 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.

The author concludes that understanding when to use state versus computed values is crucial for avoiding bugs and improving performance. The key points are:

1. If a value can be calculated from props or state, compute it during rendering instead of storing it in state.

2. Use useMemo for slow computations to avoid unnecessary re-renders.

3. Effects are meant for interacting with the outside world, not for copying state values.

Rewritten by urgent.news; may contain errors — check the original.

Read the original at dev.to →

Report a problem with this story

More in Tech

The Fix That Brought the Bug Back

I use a second iPhone at home through a stream ( the setup ): the phone's screen comes off USB as H.264, an old laptop's GPU re-encodes it, and a fork of Sunshine streams it to my main phone.

  • Bug resurfaces after initial fix, causing connection delay and termination
  • Transcoder fixes include timeout for bus threads and replays for new viewers
  • System functionality restored with updated transcoder and fallback mechanism

Interaction Nets Explained: The Programming Model Behind the Frostbite CPU

On November 18, 2026, the chip-design startup Architect Labs published a post introducing Frostbite . They describe it as a general-purpose CPU with a new instruction set.

  • Interaction nets enable Frostbite's energy-efficient programming model
  • Programs represented as graphs with nodes and wires in interaction nets
  • Frostbite's quad achieved 4.59x better performance per watt using interaction nets

Three ways to connect a WhatsApp number to Bitrix24 (and what each one costs you)

If your customers write to you on WhatsApp, sooner or later someone asks the same question: "Can we see these chats inside Bitrix24?" The answer is yes, but there is more than one way to do it, and…

  • Three methods to connect WhatsApp to Bitrix24: QR code, official Business Platform, or coexistence
  • QR code method is quick but unofficial and may be restricted by WhatsApp
  • Official Business Platform offers Meta support and message templates but requires approval

More from Saturday 21 November →