可以在没有连接的情况下发送动作吗?

Isa*_*aac 6 reactjs react-native redux react-redux

export default class App extends Component {
...
  componentDidMount() {
    //registering event listener
    BackgroundGeolocation.on('location', this.onLocation, this.onError);
  }

  onLocation(location) {
   //wishing to dispatch an action to update variable in store
  }

  render() {
    return (
      <Provider store={store}>
        <AlertProvider>
          <MainNavigator screenProps={{ isConnected: this.state.isConnected }} />
        </AlertProvider>
      </Provider>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,我不能连接到这个组件中的商店,因为我们只是配置并将商店传递给Provider.我怎么可能在我的onLocation事件中发送一个动作?

Roh*_*ali 17

您可以使用商店对象直接分派.

store.dispatch({type:"UPDATE_VARIABLE", payload:variable.value})
Run Code Online (Sandbox Code Playgroud)

如果您位于商店对象不可用的其他组件中,请从主组件中导出它并将其导入到需要它的位置并使用上述语句

  • 您从哪里导入“商店”? (7认同)