Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Working on my personal project and want to use hooks inside.
I've seen a global state implementation using hooks and my question is:
which one is better to use? Redux vs use-global-hook ?
It seems super easy to manage with the global hook, but what about performance? What are advantages, disadvantages over redux?
I've built apps using both methods and didn't notice any difference in performance; although Redux bindings are supposed to prevent unnecessary renders, I'm not sure how much of an impact this would make on the final performance of the app. Furthermore, Redux offers a time travelling debugger as well as a middleware API. The following article discusses this topic in more depth:
https://frontarm.com/james-k-nelson/when-context-replaces-redux/
If you're planning to build a sizeable project of any complexity, Redux is going to provide a feature-rich store and state management via a unidirectional flow, actions, reducers, middleware, asynchronous thunks, etc. Redux is a mature pattern (based on Flux) with a great deal of "road-testing" and a thriving community. Also with Redux you'll be able to use fully stateful React class components and leverage the component lifecycle.
Global state hooks seem like a great upcoming solution for simpler components and problems where you want to keep your components functional and pure. I don't see any signs the performance would be any different, the only way to tell would be via empirical profiling.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
Are there reasons why one would use both redux and useContext in their APP?
If so, what are they? Can you give examples of when to use either?
It is not suggested to use both redux and context api in single app.
Both context and Redux has advantages while developing the application in their own way. Depending upon the use case you need to decide whether to go with context or redux.
Context
If the application is smaller and you need to have a centralized store
that needs to store and access the datas. You can use context api. But the code
will not be that organised when compared to redux.
The below reference will provide you an outline on creating the context,
https://reactjs.org/docs/context.html#reactcreatecontext
The below will provide you the reference on using the context,
https://reactjs.org/docs/hooks-reference.html#usecontext
Redux
If the application you are developing is bigger or enterprise level
application. You can go with redux which provides more optimized code
structure compared to context api.
If you want to integrate and use redux. Use reduxtoolkit for simple and elegant usage.
Use the below link for reference,
https://redux-toolkit.js.org/introduction/getting-started
Both are excellent tools for their own specific niche, Redux is overkill just to pass data from parent to child & Context API truly shines in this case. When you have a lot of dynamic data Redux got your back!
To answer your question Yes you can use them both in the same react app but there is no reason to do so and it is recommended to use one of them depending on your needs
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am a student trying to learn web development along with the ReactJs library. But I have a huge struggle selecting between React Context and Redux state management. I have never worked on real-time huge projects. So everyone is suggesting I should go with React context. Even my friend who is working on a real-time project in the IT field is using React Context instead of Redux in their project. So he suggests I should just stick with React Context. But I think Redux was there even before React context and is really suitable for huge projects. So a lot of the older projects may be using Redux. But Redux is really hard compared to React Context.
Can anyone give me a suggestion on whether I should just stick with React Context or I should learn Redux?
Depends on the scope of your project. There can be some problems with React Context with high frequency updates, but if it is mostly low frequency ( theme changes, authentication ) then just use React Context because it's simply better if you plan to use the data only in ReactJS and it's additional packages
Actually it is a good idea to know both. I would start with React context which is much smaller and easier to grasp, and out of the box.
Then you can switch to redux - and do some work with it. You will learn both since both are not that difficult.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to start building a data management app, with some CMS like features. The backend is built on Node and frontend needs to work with its API with react.
By using hooks like useReducer and useContext, I can see the core functionality of redux implemented in React core itself.
Are there some functionalities of redux I might miss out on if I choose to not use them at all?
While useReducer sounds like it will behave like Redux does, it is an entirely different concept (well not entirely, but it isn't global), so for the time being just ignore it.
Internally, Redux uses contexts to inject data into components without passing it through the whole tree, so theoretically you could do everything that redux can do with useContext, but you would need tons of your own custom code.
Personally, I think that people are far to quick to jump to using Redux to solve simple tasks. If you have state that isn't often updated, or only updated from a single component high up in the tree, you could easily get away with using a context to deliver the data to deeper components which then access the context through useContext.
The main thing still not in React Hook is in the aspect of useContext is the functionality provided by Redux middleware. In short, if you have a complex app and need to make a series of API calls and those call may have race condition, async/sync mix up together and other complicated situation, you need the help of middleware like saga for manage those API calls in the order / way you want. React Context API still not be able to handle situation like this.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I need to implement a SPA whose components are going to interact with their local state and are minimum the cases in which these state is going to be nedded by other components. For example: the employee component contains state about an employee that only will be shared with the employee's component children but not from other different component in the site. Until now my idea is to create 'stores' in the parent component (Employee for example) implemented with hooks and Context Api, for achieving max development speed and clarity in code. What do you think?
This is ultimately subjective, and it sounds like you're already leaning towards using the Context API. We made a similar decision on our current project; the amount of data that needs to be shared across the application is quite small, so adding the additional code, dependencies and file structures for Redux did not seem worth it.
But if you are using Context, make sure you have thoroughly plotted out the data flow of all parts of your application, as it's easy to overlook something and run into a situation where getting the data outside of a particular module can become very difficult. Also consider its size. If it's a large application, the benefits of centralizing your state changes for easy testing and debugging are not negligible, and Redux can also help boost performance by automatically memoizing connected components.
Again, this is ultimately opinion-based, but I think it's a concrete and common enough concern with React apps that it bears some discussion, though this might end up being closed.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a big application with different features. What I want to know exactly is that should I overwrite all setState() functions that I have used with redux ? or in some cases I have to use redux instead ?
I mean is it bad to use redux for a simple setSate action ?
It is ultimately up to you, but Redux is pitched as "global state management", which I interpret as "the state of the app".
Generally, the way I would build my state out would be to have Redux handle the data of logical/business structures (e.g. data coming from APIs), and as such I would pass in props to the components that use said data.
Once inside the component, I would use the component state to allow the component to manage its own logic for the things that it is concerned about, such as logic needed to conditionally render things or optimistic updates.
Some have even completely removed Redux from their applications with the advent of GraphQL + Apollo, allowing them to have server-side computations for what the UI would traditionally display (e.g. providing a boolean like hasComment) so that the UI doesn't have to perform that computation (backend is usually faster at these computations, too) and simply state {hasComment && <Comment />}. Another advantage of this approach is that other UIs could use hasComment without needing to implement duplicate logic.