Nesting Redux Providers - reactjs

I want to distribute a React App as a React component. Currently it uses Redux to manage its state. If the end user also uses Redux to manage the state, there will be nested Providers. Would it be a problem or should I pass the store as a prop as Dan suggested here? I personally do not like the second way tho.
Thanks a lot

As you're building a component to be consumed like any other black-box components, using redux internally and your own <Provider> is a fine option.
This will hide the parent app's store from your component but you don't want to access it anyways; it may not exist. Any data you want must be passed in through your top-level component's props.
Likewise, you don't want the parent-application to get data from your internal Redux store through Redux and you don't want any actions to go through both stores as there could easily be name conflicts and unwanted side effects.
Most people only think of Redux as an application-level state management solution and thus the negative comments. In your case your component is big enough to have a component-wide state management solution, and thus Redux for your component is appropriate.
All that said, I realize this post is several years old, so this answer is for others that stumble across this question (as I did). I would be interested in hearing in a comment what you ended up doing and how it worked out.

Related

how to use Redux instead for useSate or how to store data using Redux

const[datas,setdatas]=useState("")
i have been using useState for storing variable and while finishing the last task my higher official told me to use Redux to store change and perform action . i read it in blog youtube videos but I can't quite get to know the necessary about redux. can someone suggest me a easy example or solution for me?
Thanking you in advance.
If you have a bunch of nested components in you app, you have to send props every time to the new nested component.
Then you feel like you want your states to be managed globally.
Otherwise you probably have to type same props again and again.
This is why Redux and other state managing modules are needed.
They put your state in a global storage and you can get that state in other component with props drilling. You only need to access global storage to get necessary state.
If your app has few components, redux or other state managing modules won't be necessary.
Redux provides a easy way to pass data through the component tree without having to pass props down manually at every level, and you should only use it for global states.
I think you can keep using useState for local states.
I would like to answer in 3 points
1. Understanding Redux
I found this (three principles) to be the most helpful way to begin understanding Redux.
https://redux.js.org/understanding/thinking-in-redux/three-principles
2. Why you have been asked to use redux?
Considering your senior has asked you to use Redux, I am assuming you are only coding a part of a larger app (already using redux) and the state in question needs to be a "global state" for use by multiple other components. If that's not the case please look at other solutions like react's context provider or simply using local state and passing it down with props where needed.
3. What else do you need to know?
If point 2 holds, I would urge you to check with your senior/team for the existing implementation of redux store, reducers, middleWare and devTools.
Note : I found redux to be a complex tool and could understand and appreciate it only after using it for a while.

Redux and React context together

I am building a web app where redux is configured and app is fairly large.
Now I want to store some user preferences which will be available as part of an API respopse.
As this data is required to the majority of components I am planning to store data in the context and wrap application using the context.
I have Few questions regarding approach.
Will considering context impact the performance?
As Redux is already configured which internally uses the Context. So should we continue to use redux for user data.
Is it good practice to use Redux and Context together.
Context and Redux are very different tools that solve different problems, with some overlap.
Context is not a "state management" tool. It's a Dependency Injection mechanism, whose only purpose is to make a single value accessible to a nested tree of React components. It's up to you to decide what that value is, and how it's created. Typically, that's done using data from React component state, ie, useState and useReducer. So, you're actually doing all the "state management" yourself - Context just gives you a way to pass it down the tree.
Redux is a library and a pattern for separating your state update logic from the rest of your app, and making it easy to trace when/where/why/how your state has changed. It also gives your whole app the ability to access any piece of state in any component.
In addition, there are some distinct differences between how Context and (React-)Redux pass along updates. Context has some major perf limitations - in particular, any component that consumes a context will be forced to re-render, even if it only cares about part of the context value.
So, yes, you can use them both in the same app, in the same way you can use a hammer and a screwdriver together on the same construction job.
For more details, see my posts:
Why React Context is Not a "State Management" Tool (and Why It Doesn't Replace Redux)
React, Redux, and Context Behavior
A (Mostly) Complete Guide to React Rendering Behavior
Will considering context impact the performance?
Since you want to share user preferences that (I guess) do not change often, you could use a React context to share that data.
But performance issues arise when you put multiple different data in one React context that update at different rates. This is because every component that uses the context will be rerendered even if the part of the context it is interessted in did not change. In this case you can split the context and use one context for each part of the data.
Since you want to use the context to share an application state, you should use Redux. In Redux you use selectors to select a part of the application state. This useSelector hook is implemented in a way that it only triggers a rerender of the component if the selected part changes. You can even pass it an equality function if you want to change the way state change is detected.
As Redux is already configured which internally uses the Context. So should we continue to use redux for user data.
I would say: yes, continue with Redux.
Since you already use Redux you should not spread your application state management over different concepts. Put the user settings in the Redux store (like any other application state) and don't handle them special.
Is it good practice to use Redux and Context together
Well, Redux is based on the React context. So if you use Redux it is already a good practice.
If you mean using both for application state management, I think you should go the Redux or the React context way. Mixing them makes it harder to understand where state is managed.

use of Context alongside Redux

I am using Redux heavily in my application. I also have a very limited number of properties stored in Context:
username of currently logged-in user
screen geometry information (to adapt whenever browser window resizes)
Is this an antipattern and should I move everything over to Redux and dispense with Context altogether? I am inclined to answering yes. So the question is:
Are there valid use cases for using both Context and Redux in the same app or is it a code smell?
No I can't think of a single reason why it would be a good idea to use both.
Generally "if you are using Redux only to avoid passing props down to deeply nested components, then you could replace Redux with the Context API".
If you need more advanced features, like predictable state container, async actions and so on, then choose Redux.
Pardon the pun, but "context switching" between the two is only going to confuse you, your app and future developers on your app.
in your case, username stuff definitely could belong in a redux reducer and screen geometry information feels like it is basic enough to live in React and be passed down as props. although, this of course can be stored in Redux state too

React components with shared state that are far away

I am new to React so please excuse me if this is a noob question but I really could not find the answer in the DOCs or elsewhere.
Let's say I have two buttons with a counter that share the state but are far away from each other in terms of the placement in the UI.
The documentation says the common owner component for both buttons should own the state. It makes sense if the components are next to each other like in the example but what if my buttons are each part of a different UI group and are far away in terms of nesting? My state holder would be the root of the document and I would have to pass a handler function down through many layers. And what if I need to add new component somewhere else that also needs to know the state? Would I have to modify all the parent components in the way to pass the state down? That is tremendously impractical.
Without React I would have some global Subscribe/Publish pattern like jQuery Observer and all UI elements could subscribe/publish to it regardless of their nesting position.
How does React solve this?
Related question: If I need to load/save the state to DB, how do I pass a reference of the controller (or Whatever) to each React component that stores the state?
1 for global state you may use REDUX
Redux is a predictable state container for JavaScript apps
for connect/subscribe component with that state ,you should use react-redux
If components are far away in terms of nesting, you may connect/subscribe them to redux store, and take only neccessary part of state. They will update if only neccessary part is changed.
article that explains how you can do your case
to learn how to use redux you can watch this videos from creator of redux (Dan Abramov)
1.getting-started-with-redux
2.building-react-applications-with-idiomatic-redux
3.I definitely recommend to you discordapp rectiflux channel. because you allways can ask any question online.(there you can find contributors of that tools)
2 alternative way that less verbose then redux is MobX
MobX is a battle tested library that makes state management simple and scalable by transparently applying functional reactive programming (TFRP). The philosophy behind MobX is very simple:
Anything that can be derived from the application state, should be derived. Automatically.
I suggest to look at the Flux stores. In short, stores are like model in your application. You can listen to change (subscribe) and also modify their properties (publish). You can see how it was done in example app.
A better option is to go with Redux.
Redux is enabling use cases like yours in a way simpler fashion :)
It will help you with all the state and make your life much easier.
Some good resources for learning:
The Redux Website
Video courses from Dan Abramov, the creator [Free]
Awesome course on Udemy [Not free]
Building Applications with React and Redux in ES6
And finally take a look at this youtube series [Free]
Managing state in the middle layers of your app should be avoided where possible. This data belongs in a store, which holds the global state of the app. Then each component accesses the state via its props.
The naïve approach to get the data down to the component is to pass the store through all the layers of your app "manually", i.e. through props.
Smarter alternatives exist, which use connected components, that access the global state through the context (as opposed to the props). Typically, the presentational component (your button component) is wrapped in a container component that handles this connection to the store, then passes the data in via props.
There are numerous frameworks that facilitate this process, links to which are already provided in the other answers.
If you are trying to share simple states, try this ( I am the author): react-provide-state
Otherwise I will recommend Redux. It has become the most popular tool for managing application states.
In the applications being working on, we use Redux to manage the main application states and almost all other states. But we use 'react-provide-state' for simple, UI only states like Modal, Checkbox states.

Am I following flux if I use browserHistory.push() inside of a nested react component?

According to the react-router documentation doing a browserHistory.push() inside of a nested react component is the accepted way to change the current route in a react-router app:
https://github.com/rackt/react-router/blob/latest/upgrade-guides/v2.0.0.md#navigating-inside-deeply-nested-components
If a route change is considered a change of state, which I'm not sure whether it is or not hence why I'm asking, would't this be anti-flux?
If a route change isn't considered a change of state, then I guess the documented way is correct.
Can someone weigh in on this?
"State" is kind of an arbitrary term. Ultimately, flux gives you the flexibility to decide where you wan to store state when there are multiple possible places to track it. Some data is managed within its own component, and other data is managed in a flux type store to share with other components. And there are a lot of things that are managed by the browser that you wouldn't keep in your application state--for example, currently selected input box. You could keep them in your flux state if you wanted, or you could keep them in your component state. Or you could let the browser handle that and not worry about it. It all depends on how you want to make these data available across your application.
The URL location is an example of a state which can be maintained by the browser outside of the flux or component state trees. You can, however include it in your store and bind to it if you feel you need it in your application in a way that would be useful. react-router-redux specifically is an example of an implementation that brings the location bar in to the flux state for redux applications.
At the end of the day, however, its up to you to determine if having that information available in your flux state is useful or not. Pedantically speaking, yes, you should have all of your data in flux to have the most pure flux implementation, but that's not really what you should be optimizing for.
The documentation for react-router is perfectly correct to use in a flux application, but if you're using something like redux, it's at least nice to have a real react-router integration so that it doesn't become something of an exception on how your application deals with state.

Resources