Made in Builder

Made in Builder.io

Live Demo 👉 All Demo, No Pitch: Content & Commerce / Builder.io & Elastic Path on 12/13

×

Developers

Product

Use Cases

Pricing

Developers

Resources

Company

Get StartedLogin

Product

Features

Integrations

Talk to an Expert

Pricing

Blog

Home

Resources

Blog

Forum

Github

Login

Signup

×

Visual CMS

Drag-and-drop visual editor and headless CMS for any tech stack

Theme Studio for Shopify

Build and optimize your Shopify-hosted storefront, no coding required

Resources

Blog

Get StartedLogin

‹ Back to blog

React Development

A Cure for React useState Hell?

January 12, 2023

Written By Steve Sewell

Do you ever find yourself in React useState hook hell?

Yeah, this good stuff:

The above is a component to update a calendar event. Sadly, it has a number of problems.

Besides not being easy on the eyes, there are no safeguards here. There’s nothing preventing you from choosing an end date that’s before the start date, which makes no sense.

There’s also no guard for a title or description that is too long.

Sure, we could hold our breath and trust that everywhere we’re calling set*() we will remember (or even know) to validate all of these things before writing to state, but I don’t rest easy knowing things are just sitting in such an easy to break state.

There is a more powerful alternative to useState

Did you know that there is an alternative state hook that is more powerful and easier to use than you might think?

Using useReducer, we could transform the above code, to just this:

The useReducer hook helps you control transformations from state A to state B.

Now, you could say “I can do that with useState too, see” and point to some code like this:

Though you’d be right, there’s one very important thing to consider here. Besides that this format still hopes that you always remember to spread on ...event so you don’t mess up by mutating the object directly (and subsequently causing React to not rerender as expected), it still misses out on the most critical benefit of useReducer — the ability to supply a function that controls state transitions.

Going back to using useReducer, the only difference is you get an additional argument that is a function that can help us ensure that each state transition is safe and valid:

This has the major benefit of guaranteeing that your state is always valid, in a fully centralized way.

So with this model, even if future code is added over time, and future developers on your team call updateEvent() with potentially invalid data, your callback will always fire.

For instance, we may want to always ensure, no matter how and where state is written, that the end date is never before the start date (as that would make no sense), and that the title has a max length of 100 characters:

This ability to prevent direct mutation of state gives us a major safety net, especially as our code grows in complexity over time.

Note that you should also provide validation in your UI as well. But think of this as an additional set of safety guarantees, a bit like an ORM over a database, so that we can be fully assured that our state is always valid when written. This can help us prevent strange and hard to debug issues in the future.

You can use useReducer virtually anywhere you’d use useState

Maybe you have the world's simplest component, a basic counter, so you are using the useState hook:

But even in this small example, should count be able to go infinitely high? Should it ever be negative?

Ok, maybe it’s not so conceivable how a negative value could be achieved here, but if we wanted to set a count limit, it’s trivial with useReducer and we can have complete confidence that the state is always valid, regardless of where and how it’s written to.

(Optionally) Redux-ify things

As things get more complex, you could even opt to use a redux style action-based pattern as well.

Going back to our calendar event example, we could alternatively write it similar to the below:

If you look up just about any docs or articles about useReducer, they seem to imply this is the one and only way to use the hook.

But I want to impress upon you that this is just only one of many patterns you can use this hook for. While this is subjective, I am personally not a huge fan of Redux and this exact pattern.

It has its merits, but I think once you want to start layering in new abstractions for actions, Mobx, Zustand, or XState are preferable in my personal opinion.

That said, there is something elegant about being able to utilize this pattern without any additional dependencies, so I’ll give people who love this format that.

Sharing reducers

One other nicety of useReducer is it can be handy when child components need to update data managed by this hook. As opposed to having to pass several functions like you would when using useState, you could just pass your reducer function down.

From an example in the React docs:

Then from the child:

This way you can not only have just one unified update function, but have safety guarantees that state updates triggered by child components conform to your requirements.

The common pitfall

It is important to keep in mind that you must always treat the state value of the useReducer hook as immutable. A number of problems can occur if you accidentally mutate the object in your reducer function.

For instance, an example from the React docs:

and the fix:

If you find you are often running into this problem, you could enlist the help of a small library:

(Optional) Common pitfall solution: Immer

One very nifty library for ensuring immutable data, while having an elegant mutable DX, is Immer.

The use-immer package additionally provides a useImmerReducer function that allows you to make state transitions via direct mutation, but under the hood creates an immutable copy using Proxies in JavaScript.

This of course is completely optional, and only needed if this solves problems you are actively encoutering.

So when should I use useState vs useReducer?

Despite my enthusiasm for useReducer, and pointing out the many places you could use it, let’s remember not to prematurely abstract.

In general, you are likely still fine using useState. I would consider incrementally adopting useReducer as your state and validation requirements begin to get more complex, warranting the additional effort.

Then, if you are adopting useReducer for complex objects frequently, and often hit pitfalls around mutation, introducing Immer could be worthwhile.

That, or if you’ve gotten to this point of state management complexity, you may want to look at some even more scalable solutions, such as Mobx, Zustand, or XState to meet your needs.

But don’t forget, start simple, and add complexity only as needed.

Thank you, David

This post was inspired by David Khourshid, creator of XState and Stately, who opened my eyes to the possibilities of the useReducer hook with his epic twitter thread:

About me

Hi! I'm Steve, CEO of Builder.io.

If you like our content, you can subscribe to us on dev.to, twitter, or our newsletter.

We make a way to drag + drop with your components to create pages and other CMS content on your site or app, visually.

You may find it interesting or useful:

Visually build with your components

Builder.io is a Visual CMS that let's you drag and drop to create content on your site visually, using your components.

Stop drowning in a backlog of requests - build with your whole team instead.

Try it out
Learn more

Share

Twitter
LinkedIn
Facebook

Continue Reading
Web Performance14 MIN
Optimal Images in HTML
WRITTEN BYSteve Sewell
January 26, 2023
web development9 MIN
The Tailwind CSS Drama Your Users Don't Care About
WRITTEN BYYoav Ganbar
January 25, 2023
Web Development9 MIN
Fast and Light Relative Time Strings in JS
WRITTEN BYSteve Sewell
January 24, 2023

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Visually build and optimize digital experiences on any tech stack. No coding required, and developer approved.

Sign up

Log in

DEVELOPERS

Builder for Developers

Developer Docs

Github

JSX Lite

Qwik

INTEGRATIONS

React

Angular

Next.js

Gatsby

PRODUCT

Product features

Pricing

RESOURCES

User Guides

Blog

Forum

Templates

COMPANY

About

Careers 🚀

Visually build and optimize digital experiences on any tech stack. No coding required, and developer approved.
Get Started
Log in

DEVELOPERS

Builder for Developers

Developer Docs

Open Source Projects

Performance Insights

PRODUCT

Features

Pricing

RESOURCES

User Guides

Blog

Community Forum

Templates

Partners

Submit an Idea

INTEGRATIONS

React

Next.js

Gatsby

Angular

Vue

Nuxt

Hydrogen

Salesforce

Shopify

All Integrations

Security

Privacy Policy

Terms of Service

By clicking “Subscribe”, I agree to Builder.io's Terms of Service and Privacy Policy.