在函数内访问app状态[重复]

时间:2022-08-22 21:56:04

This question already has an answer here:

这个问题在这里已有答案:

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。

  1. Accessing AppState.currentState to get the current state (kept updated).
  2. 访问AppState.currentState以获取当前状态(保持更新)。
  3. Adding an event listener to the changes to AppState through AppState.addEventListener(type, handler).
  4. 通过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。

  1. Accessing AppState.currentState to get the current state (kept updated).
  2. 访问AppState.currentState以获取当前状态(保持更新)。
  3. Adding an event listener to the changes to AppState through AppState.addEventListener(type, handler).
  4. 通过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.

如果您需要一次,请使用第一个。但是,如果您需要检查更改,请使用第二个。