Search code examples
reactjsreduxreact-redux

Can one retrieve data from the Store using useSelector() outside of the a component?


Can I get data from a Redux store by using useSelector() but outside the functional component in ReactJS?

I try using useSelector() as an ordinary function but it returns an an error because useSelector() is running outside of the functional react component.


Solution

  • Import your store, then call getState() to access the current state. For example:

    import store from './mystore.js';
    // ...
    const someData = store.getState().data;