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 Cross-Access State Values

Gaurav Singhal

  • Mar 5, 2020
  • 11 Min read
  • Mar 5, 2020
  • 11 Min read
Web Development


React is a popular JavaScript libraries used to create user interfaces using component-based architecture.

For managing application-level data, Redux can be implemented with different platforms, such as Angular, React, or Vue.js, to manage the global state data.

What is React-Redux?

The Redux library allows us to interact with the global store object, which consists of the global state object that can be used to access the data into the multiple components.

React-Redux is a standalone library that allows us to use Redux with a React application. They are generally used together to access the global state data.

The advantages of using React-Redux include:

  • Better component reusability due to data access across the application
  • Official library to use Redux with a React application
  • It skips the unused data and can use the useful data into the specific component
  • Global state object so that it can be accessible to multiple components
  • Giant community support

Access State Data into the Component

The React-Redux library provides a set of APIs that allows us to dispatch actions from the components and access the updated or modified data from the global store object.

React-Redux's <Provider> component uses the <ReactReduxContext.Provider> to put the Redux store and the current store state into context, and the connect() method uses <ReactReduxContext.Consumer> to read those different values and handle data updates.

We use <provider> to specify the application context like this.

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

The store is the global state object—a collection of reducers that hold the data of separate state, e.g., employee, student, teachers, and so on.

The method used to connect the components to the Redux store is called connect (), and it allows us to access the global state data into the component.

The basic syntax of the connect() method should look like this.

1function connect(
2    mapStateToProps?, 
3    mapDispatchToProps?, 
4    mergeProps?, 
5    options?

This syntax accepts the different arguments that accept mapStateToProps and mapDispatchToProps, widely used arguments to access the state values and dispatch the actions from the current component.

For example, if we want to access employee records from the global state object, we can use it like this.

1import React, { Component } from 'react';
2import { connect } from 'react-redux';
4class EmployeeMaster extends Component {
6    render() {
7        return (
8           // Return modified elements
9        )
10    }
13const mapStateToProps = state => {
14    return {
15        // To get the list of employee details from store
16        employee:
17    }
20export default connect(mapStateToProps, null)(EmployeeMaster);

Here in this example, we have consumed one argument along with the connect() method: mapStateToProps, which is used to access the state values into the current component as the props value.

Now, if we want to access the employee data, then we can use it like this.

1class EmployeeMaster extends Component {
3    render() {
4        // Accessing state values as props
5        const { employee } = this.props;
7        return (
8           // Use employee data here
9        )
10    }

We have accessed employee as the props because when we access the global state objects or values, we cannot modify it; hence it acts as a prop for the current component.

We can also use multiple state values and divide all the state values as separate props like this.

1const mapStateToProps = state => {
2    return {
3        // to get the list of employee, student and teachers details from the store
4        employee:,
5        student:,
6        teachers:
7    }

As you can see, we have three different keys as props that fetch the specific state object from the global state object and can use those objects into the current component.

We can also use ownProps if we want to access different props values from the same component as a separate argument like this.

1const mapStateToProps = (state, ownProps) => {
2    return {
3        // To get the list of employee, student and teachers details from the store
4        employee:,
5        student:,
6        teachers:,
7        temp: ownProps
8    }

The ownProps argument allows us to merge the props coming from the current component, and it wraps the final source of props into the newly added props object.

Access Cross-State Data to React with Redux

As we have learned, the Redux store maintains the global state object ,which combines the data from the different reducer and makes it available for all the components across the application.

Accessing cross-state data may be required sometimes because we have to manipulate multiple state values at a time. For example, we may need to access the student data into the teacher component where each teacher can assign to the class of students.

Note: Data coming from different reducers may or may not be useful ultimately, so we have to filter it based on our functional requirements.

For that, we can make use of the library reselect, which allows us to store the minimal possible state values.

The selectors are efficient and compatible in nature so they can be useful to others as an input value. Now, let's install the reselect library.

1npm install --save reselect

We can create the new selector using the method createSelector(), which we import like this.

1import { createSelector } from 'reselect';

The basic syntax of the reselect should look like this.

1// Basic selector
2const selector_name = (state) => state.parent.key_value

As you can see in the syntax, we can access the specific state value by using the specific key name that we are going to consume into the component.

Then we can create the selector function that accepts the first argument as a selector name, and another argument is a function that manipulates the state value and returns the updated values just like this.

1// Basic selector
2const selector_name = (state) => state.parent.key_value
4// Reselect function
5export const function_name = createSelector(
6  [ selector_name ],
7  (func) => reurn_anything

Here, the reselect function accepts the selector name as the first argument, and then after the state value will be modified into the function so that we will get the required values into the component rather than the complete state object.

For example, if we want to create the selector for three different objects, such as students, teachers, and employees, then the selector and selector functions will look like this.

1const getStudentsRecords = (state) =>;
2const getEmployeesRecords = (state) =>;
3const getTeachersRecords = (state) =>;
5// Students reselect function
6export const getStudents = createSelector(
7    [getStudentsRecords],
8    data => data
11// Employees reselect function
12export const getEmployees = createSelector(
13    [getEmployeesRecords],
14    data => data
17// Teachers reselect function
18export const getTeachers = createSelector(
19    [getTeachersRecords],
20    data => data

Here, initially, we have created three different selectors and used all those selectors as an argument along with the reselect function like this.

1// Students reselect function
2export const getStudents = createSelector(
3    [getStudentsRecords], // selector as first argument
4    data => data

As you can see in the above example, we have a selector called getStudentsRecords, which represents the students' data; hence we are allowing only student records to be selected from the complete state object.

Now once we use the selector getStudents, we will get the complete data of the student object from the global state object.

Access Multiple State Values Using the Selector Function

We have seen an example of accessing a single state value, but we can also use multiple state values and manipulate them like this.

1const selector_name = createSelector(
2  state => state.values.value1,
3  state => state.values.value2,
4  (value1, value2) => value1 + value2

In the above example, we have accessed different state values, and at the end of the callback function, the different state values are concat with each other and return a new set of data.

Access and Use Selectors from Components

After creating the various selector and selector functions, we should use those selectors into our component so it can be used as a prop into the existing component.

To access the selectors, we should understand mapStateToProps, which is used to take the entire state object and picks out the specific values.

Without the selector functions, we can access the state values like this.

1const mapStateToProps = state => {
2    return {
3        employee:
4    }
7export default connect(
8    mapStateToProps,
9    null

As you can see, we are accessing the employee state object as, so if you update the employee object into the global state object, then it will be painful to update into all the required files.

But reselect makes it possible to select only the appropriate and useful data to be fetched from the global state object into the component, like this.

1const mapStateToProps = state => {
2    return {
3        students: getStudents(state),
4        employees: getEmployees(state),
5        teachers: getTeachers(state),
6    }
9export default connect(
10    mapStateToProps,
11    null

Using this approach, the next time we update the state values, we need to update the selector so it will be reflected in all the components where those selector functions are being used.


Accessing the cross-state value from the component is a crucial task to manage the maintainability of the state data across an application.

In this guide, we have learned to make use of the reselect library to access the cross-state values from a component, which directly impacts app performance by fetching the required state values only. Hope this guide was helpful.