Giter Site home page Giter Site logo

The value of useReducer (proposal) about react HOT 4 OPEN

DioNNiS avatar DioNNiS commented on April 28, 2024
The value of useReducer (proposal)

from react.

Comments (4)

DioNNiS avatar DioNNiS commented on April 28, 2024 2

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.

TheJarX avatar TheJarX commented on April 28, 2024

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.

TheJarX avatar TheJarX commented on April 28, 2024

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:

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.

harsha-n avatar harsha-n commented on April 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.