October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

React Derived State: Why That useState Is Probably a Bug

If a React value can be calculated from current props or state, deriving it during render is usually safer than storing a duplicate in useState.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If a value can be calculated from the props and state a component already has, calculate it during rendering instead of storing it in another useState. The extra state creates a second value that must stay synchronized—and can go stale. State is still right for information that changes independently, or when you intentionally want to preserve an initial value.

What “derived state” means

Derived state is a value that can be worked out from the component’s current props or other state. A full name derived from a first and last name is a simple example. So is the object for a selected list item when the component already has the list and the selected item’s ID.

Storing both the source values and their derivation gives the component two representations of the same information. Every update must keep them consistent. If one changes without the other, the interface can show an outdated or contradictory result.

Calculate display values during rendering

When a value is determined by current props or state, declare it in the component body. React will calculate it as part of rendering, using the current inputs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function Name({ firstName, lastName }) {
  const fullName = firstName + ' ' + lastName;
  return <p>{fullName}</p>;
}

There is no need for a separate fullName state variable and an Effect that updates it when either name changes. The calculation already describes the relationship directly.

Why copying a prop into state goes stale

A prop passed to useState supplies the initial value; it does not keep the state synchronized with future prop changes.

function Message({ messageColor }) {
  const [color, setColor] = useState(messageColor);
  // ...
}

If the parent later passes a different messageColor, color remains the component’s local state value. That may be an accidental stale copy if the child is supposed to follow the parent.

If the child should always reflect the latest value, use messageColor directly. If it should start with the prop and then become independently editable, make that intent visible in the API with a name such as initialColor or defaultColor. In that design, ignoring later changes to the initial prop is deliberate.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Store a selected ID, not a copied list item

When a selection refers to an item already held in a list, keep the stable identifier in state and find the current item during rendering.

function List({ items }) {
  const [selectedId, setSelectedId] = useState(null);
  const selectedItem = items.find(item => item.id === selectedId);
  // ...
}

If an item’s details change in items, the derived selectedItem comes from the current list. A separately stored copy of the object could continue showing old details.

Use an Effect for external synchronization, not routine derivation

An Effect is appropriate when a component must synchronize with something outside React. It is not the routine way to transform props or state into a value for display, or to keep one React state variable updated from another.

If there is no external system involved and the task is simply to derive a value from current inputs, calculate it during rendering. This avoids an extra synchronization step and the stale intermediate render it can introduce.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the pattern that matches who owns the value

Need Pattern Tradeoff
The value follows current props or state Calculate during rendering Uses current inputs; calculation runs as part of rendering.
The calculation is expensive Consider useMemo Can reduce repeated computation; the result is still derived, not independently owned state.
The child should always follow its parent Use the prop directly or make the component controlled The parent remains the source of truth.
The child should preserve only an initial prop value Initialize local state from a clearly named initial/default prop Later changes to that prop are intentionally ignored.
A change of identity should reset all child state Give the component a different key React resets the keyed component’s state tree.
A selection refers to an item in a collection Store its ID and derive the current item Avoids a stale copied object.
The component must synchronize with a non-React system Use an Effect where appropriate Designed for external synchronization, not ordinary value derivation.

When a prop change should reset or adjust local state

Use a controlled component when the parent should decide

If the parent owns the value and determines how it changes, pass the value and an event handler to the child. The child displays the supplied value rather than maintaining a competing copy.

Use a key when the whole child state belongs to a new identity

If changing a prop means the child should start fresh, change the component’s key. React then treats it as a different component and resets its state tree. This is often clearer than copying the prop and manually resetting several state variables.

Update state in the event that changes it

When a user action changes a value and related state, update the relevant state in that event handler where possible. That keeps the cause and its state changes together instead of waiting for an Effect to notice a derived difference.

Adjusting state during render is a rare escape hatch

React also documents conditionally adjusting the same component’s state during rendering when a prop change must affect some local state but neither a controlled design nor a key-based reset fits. This pattern is harder to follow because it complicates data flow; it must be conditional so it does not trigger an update on every render. Most components should not need it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A quick decision check

  • If the value can be computed from the current props or other state, calculate it in the component body.
  • If it is an expensive calculation, consider useMemo as an optimization rather than adding a second independently updated state value.
  • If it identifies an existing item, store the ID and look up the current item.
  • If it should follow the parent, use the prop or a controlled component.
  • If it should retain only its starting value, name that initial/default contract explicitly.
  • If a new identity should reset the child, consider changing its key.
  • If the component must coordinate with an external system, use an Effect for that synchronization.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.