Author avatar

Gaurav Singhal

Pass Store Explicitly Into as a Prop to Connect() in React

Gaurav Singhal

  • Sep 5, 2020
  • 5 Min read
  • Sep 5, 2020
  • 5 Min read
Web Development
Client-side Frameworks
Front End Web Development


React is used to create an intuitive user interface by following a component-based architecture; hence, it renders data into the DOM. Redux is a way to manage an application-wide state so data can be accessed throughout the app using the connect() function to fetch the data from the root state object.

In this guide, you will learn how to create and pass the store as a prop to the connect() method of Redux.

Passing the Redux Store via Props

The store in Redux is an object that consists of state data for the whole application. This means the root state object resides in the store.

The store is the global state object that can be passed and accessed from any component using the method connect().

Create the store as demonstrated below.

1import { createStore, combineReducers } from "redux";
2import { todos } from "../reducers";
4export default () => {
5  const rootReducer = combineReducers({
6    todos
7  });
9  return createStore(rootReducer);

The function combineReducers accepts the list of reducers. It creates the combined state object, which represents a store by creating it using createStore() and agrees with the combined reducers object.

The next step is to configure the store in your React app.

1import "./style.css";
2import React from "react";
3import { render } from "react-dom";
4import { Provider } from "react-redux";
5import App from "./components/App";
6import configureStore from "./store";
8// Used the store object
9const store = configureStore();
12  <Provider store={store}>
13    <App />
14  </Provider>,
15  document.getElementById("root")

In the above code snippet, the store, which was created using createStore(), has been imported and then configured using configureStore().

The last step is to spread the root store object to all the components in the React app by passing it to the provider by using store props.

1<Provider store={store}>
2    <App />

So now, when you want to access the global state or store object in any of the components, you will be able to access it using the connect() method, as demonstrated below.

1export default connect(
2  ({ todos }) => ({ todos }),
3  {
4    addTodo,
5    deleteTodo
6  }

Here, connect() is the method used to fetch the data from the store, which allows the component to a global state object, and the reducer data called todos are accessed.

This way, you can create, configure, and pass the global store object and access the data by consuming the connect() method.

Creating the Custom Connect Method to Access the Store

So far in this guide, you have learned the primary way to create the store and access it using the connect() method.

You could also implement a custom connect function. For that, you need to create a custom function, as demonstrated below.

1function connectionWithStore(store, wrapperComponent, ...args) {
2  var CoonectWithWrapper = connect(...args)(wrapperComponent)
3  return function (props) {
4    return <CoonectWithWrapper {...props} store={store} />
5  }

Note that connectionWithStore accepts the store object, wrapper component, and other arguments.

After creating the function, use the connect() method with the required argument along with the wrapper component.

1var CoonectWithWrapper = connect(...args)(wrapperComponent)

And the same component will be returned along with the store props, which are used to access the global store data using a wrapper function.

Next, you can consume the above wrapper function with various arguments such as a store and mapStetToProps as given below.

1const variable_name = connectionWithStore(store, ...others, mapStateToProps)

By doing this, you will be able to access the store even with the connected component where you can pass the store on demand.

Note: Passing the store explicitly to the connected component will not be visible to other components, but you need to pass the store to make it available for all the components in your app, as given below.

1<Provider store={store}>
2<RootComponent />


Redux allows you to structure your data-access mechanism by creating a global state object using the store, making it easily accessible throughout the React app. I hope it has been helpful for you to learn how to pass the store explicitly or using a custom mechanism.