Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When and why useState is more benefic than useReducer? #587

Closed
tolotrasmile opened this Issue Feb 11, 2019 · 4 comments

Comments

Projects
None yet
4 participants
@tolotrasmile
Copy link

tolotrasmile commented Feb 11, 2019

Hi Kent.

Congratulations on the new design of your website kentcdodds.com.

Looking at the source code of your website, in the Subscribe component , you used useState hooks to handle the state of this component.

My question is, is not it more optimized to use a useReducer here instead of several useState? If not, why?

I asked myself this question by referring to your Egghead lesson here

@sapegin

This comment has been minimized.

Copy link

sapegin commented Feb 11, 2019

This post may be useful: useReducer, don't useState.

@kentcdodds

This comment has been minimized.

Copy link
Owner

kentcdodds commented Feb 12, 2019

Thanks for the question @tolotrasmile! I made it today's newsletter!

Here it is: https://kentcdodds.com/blog/should-i-usestate-or-usereducer

@tolotrasmile

This comment has been minimized.

Copy link
Author

tolotrasmile commented Feb 13, 2019

Big thanks for your answer @kentcdodds, it's awesome. I really like this question: "When do I use one over the other?" :) Thank you.

@alexkrolick

This comment has been minimized.

Copy link

alexkrolick commented Feb 26, 2019

@kentcdodds other than the "aesthetic" reasons in the linked posts, useReducer is safe when states depend on each other, but useState can end up with stale closures. A good rule of thumb (I think this was from Sebastian or Andrew on the React team) is to use reducers whenever states need to "reset" together or state changes depend on other state values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.