简体   繁体   中英

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??

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM