close

How do I access store state in React Redux?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How do I access store state in React Redux? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How do I access store state in React Redux ?

  1. How do I access store state in React Redux ?

    do I access store state in React Redux You need to use Store.getState() to get current state of your Store.
    For more information about getState() 

  2. How do I access store state in React Redux ?

    do I access store state in React Redux You need to use Store.getState() to get current state of your Store.
    For more information about getState() 

Method 1

You should create separate component, which will be listening to state changes and updating on every state change:

import store from '../reducers/store';

class Items extends Component {
  constructor(props) {
    super(props);

    this.state = {
      items: [],
    };

    store.subscribe(() => {
      // When state will be updated(in our case, when items will be fetched), 
      // we will update local component state and force component to rerender 
      // with new data.

      this.setState({
        items: store.getState().items;
      });
    });
  }

  render() {
    return (
      <div>
        {this.state.items.map((item) => <p> {item.title} </p> )}
      </div>
    );
  }
};

render(<Items />, document.getElementById('app'));

Method 2

You need to use Store.getState() to get current state of your Store.

For more information about getState() 

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment