Comments (4)
useReducer is beneficial when dealing with complex state logic and multiple sub-values; a very deep object for example. As the documentation says, it makes testing and debugging easier.
I'm trying to convey that for the described use case, useState
does a better job than useReducer
. Please see my example in the first message.
My proposal is to deprecate useReducer
and provide more examples for useState
.
from react.
Hi,
useReducer
is beneficial when dealing with complex state logic and multiple sub-values; a very deep object for example. As the documentation says, it makes testing and debugging easier. So for small and simple states useState
does the job. If you're incrementing the complexity of your state you should start thinking in useReducer
Anyway I believe this sort of questions have a better place: https://react.dev/community
from react.
I mean, yeah got your point. What I tried to say was that your concrete use case was simple enough to use useState
instead of useReducer
. No need to deprecate it; just don't use it if you don't need it. Both hooks serve different purposes. As you can see in the source code, they do work in a similar way but with a couple of differences.
You may want to check the documentation for more useReducer
examples in order to catch the idea of its purpose:
- https://react.dev/reference/react/useReducer (This one includes an example with
Immer
) - https://legacy.reactjs.org/docs/hooks-reference.html#usereducer (this one has very practical examples regarding lazy initialization and the combo with
useContext
)
Regarding to "...provide more examples for useState
". It's up to you how you're going to use useState
, I'm sure there're a lot of cool recipes out there (including yours). I mean you can make an API request in the initial state; doesn't mean you should do it tho ¯_(ツ)_/¯.
from react.
Conside if you are building an ecommerce site or something more complex, at that point of time the useReducer
will be of more use than useState. If you consider a simple increment, decrement separate module will be better option. Ensuring that the state remains consistent and predictable across all actions and components can be difficult with a separate module, especially as the application grows and evolves over time
from react.
Related Issues (20)
- Not detecting React App HOT 2
- Bug : Application crashes in Navigating reference pages HOT 2
- [DevTools Bug] Cannot remove node "1455" because no matching node was found in the Store. HOT 1
- Bug: onBlur is not firing in IOS Chrome when trigger done button HOT 3
- Bug:
- Bug: Class component with `defaultProps` not merging props correctly in `componentDidMount` HOT 2
- Bug: React-ts can't support sass, which makes people suffer from obsessive-compulsive disorder! HOT 1
- Bug: promises passed to server actions block on promise parameters
- Bug: server actions cannot be cancelled or given abort signals HOT 2
- Bug: Property 'inputType' does not exist on type 'Event' HOT 5
- Bug: error boundary does not catch exceptions from 'true' branch of an if-statement
- Bug:
- Bug: ESLint rule doesn't catch mistake with non-exhaustive dependencies
- Fix gap between footer logo and links HOT 16
- Source is not showing HOT 7
- Bug: Hydration error due to <button> HOT 6
- Bug: Search functionality not working correctly on react.dev in Safari on iOS real device (iPhone 12 Pro) HOT 2
- Bug:
- Google Translate causing "Text content does not match server-rendered HTML" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.