← blog blog / reduce-react-states-and-hooks.md

You don't need these many state variables

A lot of state variables means a lot of re-renders and useEffect Hooks. What if I told you there are computed variables in react and they could help you to reduce the number of state variables you use?

You don't need these many state variables

React is reactive because of its reactive state variables and props. What if I told you, you don’t need as many state variables as you require? If you’ve used VueJs or KnockoutJS you might know about computed variables. In react, most of the reactive states could be converted into computeds’ without involving a state variable.

Computed variables in React?

What is a computed variable? Computed variable is nothing but a value which is derived from or computed from a state variable. The value of this variable changes when there is a change in the state variable that it is dependent upon.

Use of a computed variable

Computed variables can replace your extra state variables and help you reduce your re-renders. It also reduces your useEffect hooks that you would require to update these values.

How to convert state variable to a computed value?

Consider this example where you have to show a error message based on the input value. You could see yourself writing two state variables. One for value and the other for the error state. The following code illustrates it.

//bad
const [value, setValue] = useState()
const [error, setError] = useState() // this is not required as a state

useEffect(() => {
  if (value === '') setError('Value cannot be empty')
}, [value])

return (
  <div>
    <input value={value} onChange={({ target }) => setValue(target.value)} />
    {error && <div className="error-msg">{error}</div>}
  </div>
)

Actually you could keep the error as a normal variable which would be computed during each render based on the value of the value state.

This would be the better approach ✅

//good
const [value, setValue] = useState()
// here error is a computed value
const error = value === '' ? 'Value cannot be empty' : ''

return (
  <div>
    <input value={value} onChange={({ target }) => setValue(target.value)} />
    {error && <div className="error-msg">{error}</div>}
  </div>
)

Thus we have eliminated a state variable and an unwanted useEffect hook. Mission accomplished. Likewise, in your daily work, identify which state variables are computed values and replace them with normal variables. Your app could become a lot faster.

⚡️⚡️⚡️

Thanks for reading!

I write about frontend craft, React, TypeScript, and the web. Found this useful? Let me know.

@samuellawrentz →

$ echo "enjoyed this post?" · subscribe via rss ↗

$ git log --oneline --grep="react"

More articles

cd ../blog →
  1. bb77053 How to Optimize React Components Using Computed Variables

    Mar 30, 2023 2 min read tag: reacttag: optimisation

    How to Optimize React Components Using Computed Variables
  2. 93d6d57 Frontend Performance Optimisation

    Dec 27, 2022 4 min read tag: javascripttag: performance

    Frontend Performance Optimisation
  3. c9cd4e1 How to create React Hooks? - A simple guide

    Aug 10, 2022 3 min read tag: reacttag: javascript

    How to create React Hooks? - A simple guide
  4. cea819a A Sandbox That Boots in 60ms - MicroVMs vs Containers for Untrusted Code

    Jul 06, 2026 3 min read tag: infrastructuretag: sandbox

    A Sandbox That Boots in 60ms - MicroVMs vs Containers for Untrusted Code

$ giscus --load ./comments

00:00

This helps me increase the session time of my site. Thank you!

Can you stay a bit longer?