This question already has an answer here:
这个问题在这里已有答案:
- Accessing Redux state in an action creator? 3 answers
- 在动作创建者中访问Redux状态? 3个答案
I'm currently building up a mobile app using React-Native with React-Redux. Is there a way to access a Redux state (getState()) from within a function that's not in a component?
我目前正在使用React-Native和React-Redux构建一个移动应用程序。有没有办法从不在组件中的函数中访问Redux状态(getState())?
1 个解决方案
#1
0
Per the documentation, there are two ways you can use the AppState.
根据文档,有两种方法可以使用AppState。
- Accessing
AppState.currentState
to get the current state (kept updated). - 访问AppState.currentState以获取当前状态(保持更新)。
- Adding an event listener to the changes to AppState through
AppState.addEventListener(type, handler)
. - 通过AppState.addEventListener(type,handler)向AppState的更改添加事件侦听器。
If you need it once, use the first one. However, if you need to check for changes use the second.
如果您需要一次,请使用第一个。但是,如果您需要检查更改,请使用第二个。
#1
0
Per the documentation, there are two ways you can use the AppState.
根据文档,有两种方法可以使用AppState。
- Accessing
AppState.currentState
to get the current state (kept updated). - 访问AppState.currentState以获取当前状态(保持更新)。
- Adding an event listener to the changes to AppState through
AppState.addEventListener(type, handler)
. - 通过AppState.addEventListener(type,handler)向AppState的更改添加事件侦听器。
If you need it once, use the first one. However, if you need to check for changes use the second.
如果您需要一次,请使用第一个。但是,如果您需要检查更改,请使用第二个。