我目前正在使用此模式,但感觉有点不对劲。我不认为redux thunk应该返回字符串,并且组件只能访问通过连接的道具获取的数据。我可能是错的,但是有没有更好,更惯用的方式呢?
我想将两个thunk分开,以便可以分别给每个thunk打电话。
// actions
const putCustomer = ...;
const putBooking = ...;
// thunks
export async function fetchCustomer(customerId) {
return dispatch => {
const customer = await customerApi.fetch(customerId);
dispatch(actions.putCustomer( {customer} ));
};
}
export async function fetchBooking(bookingId) {
return dispatch => {
const booking = await bookingApi.fetch(bookingId);
dispatch(actions.putBooking( {booking} ));
// I AM RETURNING THE BOOKING DATA HERE
return booking;
};
}
// component
class MyComponent extends React.Component {
async componentWillMount() {
const booking = await this.props.dispatch(fetchBooking(this.props.bookingId));
// I WANT TO ACCESS THE BOOKING DATA HERE
this.props.dispatch(fetchCustomer(booking.customerId));
}
}
问题已更新