这里有一个例子:如何以正确的方式使用调度?
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 –