什么时候在 action creator 中使用 dispatch?
When to use dispatch in an action creator?
我在 ActionCreator.js 文件中定义了两个函数
第一:
export const getAudioForVerification = ()=>{
return fetch(baseUrl+'audio',{
// Get Request
}
.then(response=>response.json());}
第二个:
export const audioVerificationResult = (audioId,verificationResult) =>(dispatch)=>{
return fetch(baseUrl+'audio',{
// PUT Request
})
.then(response=>response.json());
}
主要功能:
const mapDispatchToProps = dispatch => ({
getAudioForVerification: ()=>dispatch(getAudioForVerification),
audioVerificationResult: (audioId,verificationResult)=>dispatch(audioVerificationResult(audioId,verificationResult))
});
Q1:如果我从我的第二个函数 audioVerificationResult 中删除调度,我会收到错误消息
Actions must be plain objects. Use custom middleware for async actions.
为什么第一个函数也没有出现这样的错误?
Q2:第一个函数 returns 是一个 promise(我可以在调用此函数后在我的 MainComponent 中使用 .then),而第二个函数则不是。为什么?
我最近开始学习 Promises、Redux 和 Thunk(一般的 Web 开发人员)。如果问题太宽泛,请指导我学习资源。
感谢您的宝贵时间。
您的 ActionCreator.js
中的函数不完全是 action creators,因为它们不会像 redux docs 所建议的那样返回 plain object
。它们只是js函数。
如果您在组件中使用 .then()
并阅读 results/data
,则不需要 dispatch
。 dispatch
仅帮助您进行 redux store
更新。
希望能帮助您更好地理解有关中间件的详细信息。
我在 ActionCreator.js 文件中定义了两个函数 第一:
export const getAudioForVerification = ()=>{
return fetch(baseUrl+'audio',{
// Get Request
}
.then(response=>response.json());}
第二个:
export const audioVerificationResult = (audioId,verificationResult) =>(dispatch)=>{
return fetch(baseUrl+'audio',{
// PUT Request
})
.then(response=>response.json());
}
主要功能:
const mapDispatchToProps = dispatch => ({
getAudioForVerification: ()=>dispatch(getAudioForVerification),
audioVerificationResult: (audioId,verificationResult)=>dispatch(audioVerificationResult(audioId,verificationResult))
});
Q1:如果我从我的第二个函数 audioVerificationResult 中删除调度,我会收到错误消息
Actions must be plain objects. Use custom middleware for async actions.
为什么第一个函数也没有出现这样的错误?
Q2:第一个函数 returns 是一个 promise(我可以在调用此函数后在我的 MainComponent 中使用 .then),而第二个函数则不是。为什么?
我最近开始学习 Promises、Redux 和 Thunk(一般的 Web 开发人员)。如果问题太宽泛,请指导我学习资源。
感谢您的宝贵时间。
您的 ActionCreator.js
中的函数不完全是 action creators,因为它们不会像 redux docs 所建议的那样返回 plain object
。它们只是js函数。
如果您在组件中使用 .then()
并阅读 results/data
,则不需要 dispatch
。 dispatch
仅帮助您进行 redux store
更新。
希望