简体   繁体   中英

How to get state outside a react component (using redux tool kit)

I'm working on a sizable react-redux application. Without using a react component or useSelector hook, how can I get the state from the React Redux Toolkit store?

I have already used useSelector hook in other places in my app which is working well but now I need to make use of the state value outside my react component. I can't use useSelector outside a functional component.

I found this method online but I am not sure if it is safe to use it OR there is a better route to solve this problem.

 import store from "./store" const { var1, var2 } = store.getState();

Thanks for your support.

You can use a selector with getState

const var1 = var1Selector(store.getState())

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