可以在没有连接的情况下调度操作吗?


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>
);
}
}

据我了解,我不可能连接到此组件中的存储,因为我们只是配置并将存储传递到提供程序中。如何在onLocation事件中调度操作?

您可以使用存储对象直接调度。

store.dispatch({type:"UPDATE_VARIABLE", payload:variable.value})

如果您在存储对象不容易获得的其他组件中,请将其从主组件导出并将其导入到需要它的位置并使用上述语句

react-redux有自己的钩子来调用它们

const dispatch = useDispatch();
const state = useSelector((state) => state);

不要忘记导入它们

import { useDispatch, useSelector } from 'react-redux';

vs 代码将自动完成,如果你写 useDispatch

您可以使用此代码并在类中添加构造函数,如下所示

constructor(props) {
super(props);
}
onLocation() {
this.props.dispatch({type: GET_THINGS_REQUESTED, payload: value});
}

UmiJS 3.x用户可以执行以下操作:

import { getDvaApp } from 'umi';
const dispatch = getDvaApp()._store.dispatch;
dispatch({ … })

相关内容

最新更新