redux中定义request请求回调
export function createAction(options) {
const { url, payload, method, fetchOptions, cb, type } = options
return (dispatch) => {
return fetch({ url, payload, method, ...fetchOptions }).then((res) => {
dispatch({ type, payload: cb ? cb(res) : res })
return res
})
}
}
外部调用:
export const dispatchHome = payload => createAction({
url: API_HOME,
type: HOME_INFO,
payload
})
此时返回为:
(dispatch) => {
return fetch({ url, payload, method, ...fetchOptions }).then((res) => {
dispatch({ type, payload: cb ? cb(res) : res })
return res
})
}
在home里调用:
this.props.dispatchHome().then(() => {
this.setState({ loaded: true })
})
这里默认传入dispatch参数,.then()处理可以传入res