Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Gaurav Singhal

How to Nest Smart Components in Redux

Gaurav Singhal

  • Nov 15, 2019
  • 4 Min read
  • 3,534 Views
  • Nov 15, 2019
  • 4 Min read
  • 3,534 Views
Web Development
React

Introduction

Nesting smart components is a popular design pattern for building React-Redux applications. Often you'll find yourself in a situation where you have to decide whether to connect all the components to the redux store or just connect the wrapper component and pass down the props to the child components. In this guide, we are going to learn how to nest containers, that is, smart components to build high-performing applications.

The Problem

While working with React and Redux libraries, you may come across a situation where you want to connect a child component of an already connected component, and the first questions that pops into your mind are, "Will this cause any re-renders in the UI?" or "Will this affect the performance of my app?"

Looking for an answer? It's a big fat NO! If anything, this boosts the performance of your application.

When you have a container (connected component) with 100 presentational components nested down its component tree, you will have to extract all the props required by those components and pass them down the component tree. This may affect the performance of the application, as providing props and extracting back the updated prop can be a huge task.

The Solution

Instead of passing down the props from a container, each component can be connected to the store using the connect() method and secure its props using mapStateToProps(). That way, there is less of a burden on the parent container.

The connect() method is highly optimized, and it checks for different cases to decide whether the container needs to be re-rendered. This way, it avoids unnecessary re-renders that would otherwise profoundly affect performance.

Let's begin with the code!

Wrap the Root Component with <Provider />

In order to access the global store object, we must wrap the root component with the <Provider /> component from the React-Redux library.

index.js

1// ...
2import { createStore } from "redux";
3import { Provider } from "react-redux";
4
5import MainContainer from "./MainContainer";
6
7import reducer from "./reducer";
8
9const store = createStore(reducer);
10
11function App() {
12  return (
13    <Provider store={store}>
14      <MainContainer />
15    </Provider>
16  );
17}
18
19// ...
jsx

Finishing Up the Containers

Next, we will create two components nested in the main component and connect them all to the redux store.

MainContainer.js

1import { connect } from "react-redux";
2
3const MainContainer = props => {
4    return (
5        <div>
6            <p>MainContainer Data {props.data}</p>
7            <ChildContainer>
8        </div>
9    );
10};
11
12const mapStateToProps = state => ({
13  data: state.mainContainerData
14});
15
16export default connect(mapStateToProps)(MainContainer);
jsx

ChildContainer.js

1import { connect } from "react-redux";
2
3const ChildContainer = props => {
4    return (
5    <div>
6        <p>ChildContainer Data {props.data}</p>
7        <GrandChildContainer>
8    </div>
9    );
10};
11
12const mapStateToProps = state => ({
13  data: state.childContainerData
14});
15
16export default connect(mapStateToProps)(ChildContainer);
jsx

GrandChildContainer.js

1import { connect } from "react-redux";
2
3const GrandChildContainer = props => {
4  return (
5    <div>
6      <p>GrandChildContainer Data {props.data}</p>
7    </div>
8  );
9};
10
11const mapStateToProps = state => ({
12  data: state.grandChildContainerData
13});
14
15export default connect(mapStateToProps)(GrandChildContainer);
jsx

That is how we connect each of the components to the redux store. We did not pass any props from one component to another.

Conclusion

I hope you liked this guide on nesting smart components in React-Redux. A quick recap: Nesting smart components can help you build high-performaning applications, as there is no overhead from passing props down the component tree. This design pattern is great for large-scale applications that have a high number of components in their application.