這裏有一個例子:如何以正確的方式使用調度?
export const fetchPosts = (path, postData) => {
let url = target + path + Tool.paramType(postData);
return dispatch => {
dispatch(requestPosts(postData));
return fetch(url,{
mode: 'cors',
"Content-Type": "application/json",
})
.then(response => {
if(response.ok){
response.json().then(json => dispatch(receivePosts(path, json)))
}else{
console.log("status", response.status);
}
})
.catch(error => console.log(error))
}
}
時,我想請我的commponent數據
:但是
this.props.fetchPosts(this.props.seting.url,this.props.seting.data)
,當我輸入這樣的:
import *as action from '../../Redux/Action/Index';
action.fetchPosts(this.props.seting.url,this.props.seting.data)
項目似乎開始成功......是嗎?..... =。=
這有什麼意義?您將動作映射到容器組件中的道具。你爲什麼要這樣做呢? –
簡而言之,第一種方法是正確的 - 不要在所有組件中單獨導入動作。那不建議 –
當我嘗試第二種方法時,似乎沒有錯誤,現在我知道缺點,thx分享@Arshabh Agarwal –