javascriptreactjsreduxreact-reduxstates

How to get the state from Redux store


So i was creating the Redux store in the app.js file. I also pushing many data from the database to the initial state of the Redux store.

Now I am creating a plain js file ( not React class ). Now i was confused how to get the state of the store in this file.

If this is a React class , I can use the function mapStateToProps to map the redux state to the class props, But this is just a js file with functions , i dont want to create a whole React class for this.

Is there any ways??


Solution

  • create the store in a separate file , if you're using ES Modules export it , now in any file in your project you can import it and use it

    import store from './store' if you use export default if you just export it use

    import {store}from './store'

    now you can use the store methods and functions

    store.getState() this you'll return your app state

    store.dispatch() to dispatch and action

    you can discover other methods in the redux apis page