How to pass Redux store to components imported from packages? - reactjs

Say you have a project split up over two repo's: main and account.
From the main repo I install the account repo as a dependency and import the account components.
When I create a Redux store in the main part of the app, how does one go about passing that store to the account components? My idea is to connect a container component in the main part and pass all state/actions needed in a single object. Then in the account part I'll create a Redux context feeding this object to all components that need it.
This will work, but it feels like I'm reproducing the Provider/connect functionality from the 'react-redux' package. However, I can't connect in the account part directly since the Provider is located in the main part. Can anyone think of a more elegant solution?

Read this article: https://redux.js.org/recipes/isolating-redux-sub-apps/ . Basically you can create multiple providers and separate your application so that each part has its own redux tree.
If you need to nest them then in v6 you could do something like this: https://react-redux.js.org/using-react-redux/accessing-store#multiple-stores , don't know if that is the case with redux v7 since it doesn't use context anymore.

Related

Next.js. Where to put a global provider?

I'm working with Next.js and I need to implement i18n. It requires me to add some global provider that should wrap the whole app. Also, it must receive updates from some state management (eg. Redux) to properly re-render the whole app when the active Language changes.
In the standard React app we have App.js that we put into index.js and keep all logic/providers here.
But in the case of Next, I'm not sure where to put this logic. I double-checked their documentation but I didn't find any mention about it.
Only about _app.js and _document.js but actually both don't have a possibility to be connected to Redux, etc. Actually they weren't designed for this.
I'm just curious if Next provides some official way to do it or should I just manually create some HOC as App just and wrap the whole app by myself?
Btw. I barely understand the difference between _app and _document. So I'll appreciate any clarification as well!
Please read the documentation more carefully then, you can find Keeping state when navigating pages on the second point here.
That's what redux used for.
I'm able to connect my entire app to redux and i18n from _app.js file. In my previous project, my teammates even use _document.js file to connect i18n.
As you can see from their doc, the purpose of this _app.js is to override and control the page initialization. So, you can receive updates from redux and put the changes on IntlProvider right before rendering your page.
Other solution is like what you have said, by creating some HOC to wrap your app.
The choice is yours.
You can use next-connect-redux package.
Github page
Example repo

What's the difference between React App and React Component

We will be doing our first project using React.
It will not be a Single Page App, but a Multiple Page App.
What I'm trying to figure out at the moment is : what's the difference between a component and an app.
If I only use components, can I still use Redux to have some state management on the current page ? Or do I need an app for this ?
Thanks for the information you can bring !
THoma
There is no special object called "React App". React Components build an "React App" by coming together.
But React Components are formed like tree structure. That means each component have a parent component so you can create a React Component that named "App" and can put another components inside it.
You don't need redux for state management in React Components.
I hope the answers have helped.
Your app may contains a single component and still it will be a react App. If you are using multiple components in a page you can still use react-redux. Redux is basically a container for your states and let suppose you need some state from one component to be consumed in another, Redux provide you a mechanism to make the communication efficient and predictable.
You can also look at the React Context APIs as an alternate to Redux.
An app is simply a component that holds the root of the work you are trying to do. For example an App may have the navigation menu, testimonials, adverts, content, login avitar etc.
If you are making a single App per page (For example a testimonial) then you would still have a SPA. For example, adding testimonials, searching, editing.
You should only use Redux if you are using a SPA with lots of different parts with data in common. If you are making a one-app-per-page and there is no cross over in data then you can simply using Reacts State/Props to hold your data.
Redux is good, but it forces you into a complex path your should try to avoid. If you find yourself wanting data from different domains (customers address and a list of testimonials) then you should use Redux.
If this is a new applications (green) then I strongly recommend you build the whole thing within a SPA using React-Router to control components. you can use frameworks like Next.JS to ensure the site remains small in size (dynamically loading script only when required).

Slowly implementing redux to an existing React.js project

I want to migrate my project from plain react to react redux, I am not new to React but new to Redux.
I have a fairly big web app written in React, dozens of React.js files.
most of them containing state's + passing variables between them.
including allot of Post/Get requests functions, implemented into at least half of my files.
I want to slowly move from plain react to react redux.
I wanted to ask if anyone have some article or can give an insight about migrating existing react project to react-redux.
I dont want to stop development for the sole purpose of the change but instead to slowly adapt to it.
is it possible ? is there a tool to help me do it ?
I saw some redux examples where entire render of app.js was surrounded by <Provider> </Provider>, does that mean every component inside <Provider> bracelet can not have it's own state ?
can I simply keep my old components as they are and put new ones into <Provider> </Provider> ?
Thanks in advance!
I saw some redux examples where entire render of app.js was surrounded
by , does that mean every component inside
bracelet can not have it's own state ?
The way react-redux works is by exposing a store prop, provided by the Provider. In order to consume it, or extract data from it you must wrap your component by it (not directly necessarily, but one of the parents must be a provider). In general in most apps you would simply wrap the entire application with a Provider, because for the most part, if you've chosen to introduce redux into your application, it is probably because your entire app needs some store.
Using redux does not mean that components can't have state. There is a big difference between global state - something that should be accessible to every component in your app (if the component chooses to "consume" it), and state that is private to a component - e.g. form changes before being sent to the server.
can I simply keep my old components as they are and put new ones into ?
Well, yes. But also - no. As I said earlier, you should probably start from the top and slowly drill down. Wrap your app with a Provider, and start moving your application state from the top-most component to the store. Once you get more comfortable with redux in general, you should start replacing the props you pass down the component tree with props from the state by connecting your inner components.
This way you can do it one component at a time without breaking existing logic.

What's the relationship between <Provider> and connect() in React-Redux?

I'm brand new to Redux and I'm trying to figure out the relationship between <Provider> and connect().
As I understand it, connect connects your component to the store. However, nowhere in the function arguments do you tell connect where exactly that store is!
If I'm not mistaken, the store is automagically provided to connect() by the <Provider>. This to me seems very counter-intuitive, because the entire point of Redux is to be transparent.
So my question is, how does <Provider> pass the store off to connect() without using some sort of global variable? Does it traverse the entire tree, searching for connected components and then inject itself? Is that not inefficient? And if so, how would I use two different stores within the same component tree?
Secondly, supposing I don't want to use <Provider>, how can I use connect() without it? i.e., how can I explicitly pass a store to each connected component?
<Provider> and connect are part of the react-redux module. They work together, you shouldn't really use one without the other. You can use redux on its own without react-redux, but you'll probably end up re-creating some or all of the features that react-redux provides.
react-redux works by using the React context. Context is like a hidden layer for passing variables that are shared by multiple components without explicitly passing them. To use context, you need to set the context somewhere, but also, any component that wants to use something from the context needs to get the variable. In react-redux <Provider> essentially saves the store to the context and connect provides a way to get the store from the context.
If you haven't already, I recommend these videos for getting started with Redux and react-redux from the creator of Redux.
The redux docs are pretty great and have some information regarding Provider and connect()
The option we recommend is to use a special React Redux component
called <Provider> to magically make the store available to all
container components in the application without passing it explicitly.
You only need to use it once when you render the root component
Essentially it leverages the use of context which is from React. As per the docs this allows you to pass data through the component tree without having to pass the props down manually at every level.
There's no reason why you can't explicitly pass the store. The idea here is that it just makes things easier.

What would be the correct way to communicate with redux-react application from another js module

I have a complex form built with react-redux, and I need to know whether this form is valid from another javascript on the same page. Some jquery module.
The easiest way would be add valid/not_valid dynamic class to the react form, and to do something like
$('#myform').hasClass('valid')
But let's say that validation is not the only thing I need from this react app and I want to build a kind of interface for that with some getters. e.g.:
isValid(), getTitle, doSomethingElse methods.
what would be the right way to do that?
Ideally you would store any information you need outside the React application within the store, then simply access the store within whatever jQuery you are writing. This would also work going the other direction, update the store with any particular data that the React application might need to be aware of.
One possible approach:
You can try saving the reference to the store that you instantiate in a global variable.
E.g. Let's say in your top-level React compnonent App.js you do
import MyMainStore from '..../MyMainStore';
.
.
.
<Provider store={MyMainStore}>...</Provider>
//Then finally also do
_MyApp.ReduxStore = MyMainStore;
That way in the non-react/redux part of your app you can access the state via -
_MyApp.ReduxStore.getState().myFormReducer.someExpectedState
Look forward to hearing if that works for you.

Resources