DEV Community

Cover image for How to Easily update an Object that's in state in React
Noel Ethan Chiwamba
Noel Ethan Chiwamba

Posted on • Originally published at dev.to

6

How to Easily update an Object that's in state in React

Liquid syntax error: 'raw' tag was never closed

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (4)

Collapse
 
vishal8888a8 profile image
vishal giri

Good one but I think spread operator is the best way to update react state in such case

Collapse
 
noel_ethan profile image
Noel Ethan Chiwamba

I understand there are a couple of ways to achieve the same, I just wanted to make it basic. Thank You.

Collapse
 
tealover418 profile image
Stijn de Ligt

You should look into useReducer. It's an alternative to useState but for more complex objects like what you have here.

Collapse
 
noel_ethan profile image
Noel Ethan Chiwamba

Thank you. However I just wanted to keep it basic, but i will consider useReducer too as a Scale up for the article.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay