如何在redux中等待数据

nl *_*pkr 4 reactjs redux react-redux

例如,我有2个组件.在第一天我在服务器上执行异步调用,并在调度操作后将响应放入redux存储.在第二个组件中,我想等待响应来存储并做完一些工作.

第一个组件看起来像

const response = await api.call(); // async call on server
dispatch(putToTheStore(response)); // put data to the store
Run Code Online (Sandbox Code Playgroud)

第二个组件看起来像

await props.response; // here i want to wait data come to store

// and after it actually come to the store, do some work
Run Code Online (Sandbox Code Playgroud)

可能吗 ?

for*_*ert 6

如果你的第二个组件连接到应用商店react-reduxconnect功能,你不需要做任何明确的更新组件.您添加到商店的数据将仅作为组件可用于组件prop.

如果你需要(同步)对加载的数据做一些工作,例如应用过滤器,或做一些排序,我建议直接在mapStateToProps函数中执行此操作,因为每次商店更新时都会自动调用此函数来计算新的props为您的组件.

如果您需要在第二个组件中执行一些额外的工作,即根据先前的异步调用加载更多数据,则需要实现该componentWillReceiveProps功能.