简体   繁体   English

如何一个接一个地调用顺序动作,并确保两者都被渲染(react-redux)?

[英]How to call sequential action one after another — and make sure both get rendered (react-redux)?

I want to add an item using a POST request, then close the pop-up window and update the item list with a GET request. 我想使用POST请求添加项目 ,然后关闭弹出窗口并使用GET请求更新项目列表 Using redux-thunk , I could call the 2 latter actions after the getting result from POST . 使用redux-thunk ,我可以在POST获得结果之后调用后面的两个动作。

Now I want to call loadAll action to populate the updated list of item and finish the state change/rendering before calling toggleAddItem , which actually close the pop-up window. 现在,我想调用loadAll操作来填充项的更新列表,并在调用toggleAddItem之前完成状态更改/渲染,该操作实际上关闭了弹出窗口。

export const loadAllItems = items => ({
    type: LOAD_ALL_ITEMS,
    payload: items
});

export const toggleAddItem = status => ({
    type: TOGGLE_ADD_ITEM,
    payload: status
})

export const loadAll = () => (dispatch => {
    axios.get('/getAllItems').then(res => {
        dispatch(loadAllItems(res.data))
    })
    .catch(err => {
        console.log(err);
    })
});

export const addItemAndRefresh = input => ((dispatch, getState) => {
axios.post('/addItem', input)
  .then(() => {
      axios.get('/getAllItems')
      .then(res => {
          dispatch(loadAll(res.data))
      })
      .then(() => {
          dispatch(toggleAddItem(false));
      })
  })
  .catch(err => {
      console.log(err);
  })
});

With the above code, I manage to call the loadAll before toggleAddItem . 通过上面的代码,我设法在toggleAddItem之前调用loadAll However, with componentDidUpdate log, I realize that toggleAddItem actually fires when loadAll is still updating state/rendering and even finish before the former action. 但是,有了componentDidUpdate日志,我意识到当loadAll仍在更新状态/渲染甚至在前一个操作之前完成时, toggleAddItem实际上会触发。

So far, this is my best attempt, however, I'm not sure if this sequence can avoid all issues. 到目前为止,这是我的最佳尝试,但是,我不确定此顺序是否可以避免所有问题。 There are previous cases when pop-up window is closed, and state is updated with new items. 在以前的情况下,关闭弹出窗口,并使用新项目更新状态。 However, new item list is not rendered (I suspect that this happens because I update the state while the rendering function is working, so it skips the second render). 但是,不会渲染新的项目列表(我怀疑发生这种情况是因为我在渲染功能运行时更新了状态,因此它跳过了第二个渲染)。

What would be the best implementation for dispatching consequential actions and make sure that both will be rendered? 分派后续动作并确保同时执行这两种动作的最佳实现是什么?

You need to call then on the promise returned from the loadAll action. then ,您需要调用loadAll操作返回的promise。

Firstly return that promise by changing your loadAll to: 首先,将您的loadAll更改为:

export const loadAll = () => dispatch => {
    return axios.get('/getAllItems')
      .then(res => {
          dispatch(loadAllItems(res.data))
      })
      .catch(err => {
          console.log(err);
      })
};

Then in your mapDispatchToProps call then and then pass a function which will be called when loadAll resolves. 然后在你的mapDispatchToProps打电话then再传递时将被调用的函数loadAll解决。

const mapDispatchToProps = dispatch => ({
  addItemAndRefresh: dispatch(loadAll())
                      .then(() => dispatch(toggleAddItem(false)))
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM