Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Connect or pass data as props to children

I am new to react and redux. I have a scenario where there are nested components like this.

A > B > C > D

There is a property used in A component and it will be used in D component. So, I have two approaches:

  1. Get state from redux store in component A and then pass it along as props to all it's child components even though it will be used only in D component.
  2. I should connect to redux store in component D and fetch that property from there.

What is the correct approach?

like image 370
maria zahid Avatar asked Nov 15 '17 05:11

maria zahid


People also ask

How do you pass a prop to child component?

There is no way to pass props up to a parent component from a child component. We will revisit this caveat later in this tutorial. It's also important to note that React's props are read only (immutable). As a developer, you should never mutate props but only read them in your components.

How do you pass Props to children prop in React?

Passing Props to Children in React To display them, you need to include props. children or this. props. children (for class components) in your return statement.

How do I pass data from parent to child response?

To pass data from child to parent component in React: Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .

What is the object used to pass data from parent to child?

Passing Data From Parent to Child When you need to pass data from a parent to child class component, you do this by using props. As you can see, the parent component passes props to the child component and the child can then access the data from the parent via this. props.


2 Answers

As Dan Abramov, author of redux says in this issue

Both approaches of passing props down to children or connecting them to the store are appropriate, however having nested connect() components is actually going to give you more performance. The downside is they're slightly more coupled to the application and slightly harder to test, but that may not be a big issue.

He has also articulated a nice rule of thumb to follow on reddit

I do it this way:

  • Start by using one container and several presentational components
  • As presentational component tree grows, “middle” components start to pass too many props down
  • At this point, I wrap some leaf components into containers so that “middle” components don’t need to accept and pass down props that are completely unrelated to them
  • Repeat

He has even tweeted regarding this:

Try to keep your presentation components separate. Create container components by connecting them when it’s convenient.Whenever you feel like you’re duplicating code in parent components to provide data for same kinds of children, time to extract a container.

So in simple words:

You can use connect() at any level. Doing so makes the component smart, since it knows where its props come from. A dumb component just has props, and they could come from anywhere. A smart component is coupled to redux; a dumb component is not.

UPDATE: react-redux v7 and above

The same concept applies to useSelectors too. You can receive data in a container component and pass on to your presentational components, if multiple of its children make use of the same data

If however the data used by the children is different, you can choose to use useSelector individually within the child component. This will make sure that only those components re-render which actually need to

like image 112
Shubham Khatri Avatar answered Sep 30 '22 16:09

Shubham Khatri


I would suggest if you are already using redux in your app then set the property in the redux store and fetch it in the component D.

But if the work flow is really simple and all the data is fetched from a single source per view, you can avoid redux as it is for complex state management.

like image 36
Manas Avatar answered Sep 30 '22 17:09

Manas