Programming Languages

React Derived State: When a Calculation Does Not Need an Effect

Huwell Consultancy editorial2 min read

Suppose a React screen receives a first name and a last name. It needs to display the full name. You do not normally need another state value and an Effect just to combine those two strings.

Keep the example simple

function PersonName({ firstName, lastName }) {
  const fullName = `${firstName} ${lastName}`.trim();
  return <span>{fullName}</span>;
}

The displayed value is calculated from the current props during rendering. There is no separate full-name state to keep in sync. React’s official guide explains that Effects are for synchronising with external systems, not usually for transforming data for display.

Why this matters

If you store the combined name separately and update it in an Effect, you add another update path and an extra render. For a simple calculation, that makes the component harder to follow without adding useful behaviour. This does not mean all Effects are wrong; the purpose of the code matters.

Practise with a list

Take a small list of products and a search term. First write down what should be displayed when the term is empty, matches one item or matches nothing. Then calculate the filtered list from the current inputs. Keep the example free of network requests so you can focus on the display logic.

A useful interview question

Before adding state, ask: can this value be calculated from information the component already has? Explain your answer with the example. That gives a Malta frontend interviewer a clear view of your reasoning without relying on framework buzzwords.

Sources and further reading

Browse current technology jobs in Malta →

EXPLORE THE NEXT STEP

Discuss the work you want to do.

Browse current technology vacancies with Huwell Consultancy and ask us about the responsibilities that matter to you.

Explore opportunities
KEEP EXPLORING

More guidance for your next decision