在我用于示例的代码中,我有一个用于添加新属性的属性表单,在它将其保存在数据库中之后,我想用新属性更新redux存储,并将用户发送到属性页面,其中列出了所有属性,但我想在分派(addProperty(SavedProperty))之后执行此操作,然后我可以执行history.push("/properties")。我怎么能在那里等到调度结束。我正在使用react-redux中的useDispatch。
async function handlePropertyFormSubmit(values) {
const savedProperty = await savePropertyGraphQlPromise(values);
if (savedProperty) {
dispatch(reset("property"));//reset property form
dispatch(addProperty(savedProperty));
//here I want to wait the dispatch finish and then send user to the properties page.
history.push("/properties");
}
}发布于 2020-02-25 01:51:18
用setTimeout包装您的第二个分派,类似于这个firstDispatch() setTimeout(secondDispatch)
发布于 2020-02-25 01:51:55
您可以使用类似分派的dispatched: false创建一个‘dispatch’,并在完成您的signal时将其设置为true (如果您打算在更多组件中使用此属性,则在重定向后取消设置它)。如果您使用的是react路由器,则可以将<Redirect to="yourNewURL" />呈现为重定向(基于redux store state有条件地呈现它)。
发布于 2020-02-25 02:14:03
看起来你正在使用async/await,所以你也应该能够使用它来等待调度。
async function handlePropertyFormSubmit(values) {
const savedProperty = await savePropertyGraphQlPromise(values);
if (savedProperty) {
await dispatch(reset("property"));//reset property form
await dispatch(addProperty(savedProperty));
history.push("/properties");
}
}如果addProperty操作返回一个promise (比如thunk),那么您也可以使用then方法。
dispatch(addProperty(savedProperty)).then(() => {
history.push('/properties')
})https://stackoverflow.com/questions/60381315
复制相似问题