Javascript 反应:我可以不将参数传递到动作中吗?
如果我的措辞不对,请原谅。我现在正在学习React,在将参数传递到动作时遇到问题。行动如下:Javascript 反应:我可以不将参数传递到动作中吗?,javascript,reactjs,Javascript,Reactjs,如果我的措辞不对,请原谅。我现在正在学习React,在将参数传递到动作时遇到问题。行动如下: export const getPosts = filterQs => { return dispatch => { const apiUrl = wpApi.listPosts; if (filterQs) { apiUrl += "?" + filterQs; } // Actually
export const getPosts = filterQs => {
return dispatch => {
const apiUrl = wpApi.listPosts;
if (filterQs) {
apiUrl += "?" + filterQs;
}
// Actually get the posts
dispatch(getPostsStarted());
axios.get(`${apiUrl}`)
.then(response => {
dispatch(getPostsSuccess(response.data));
})
.catch(error => {
dispatch(getPostsFailure(error.message));
});
}
}
跳过包含if(filterQs)的条件,因为在调度操作时,它似乎实际上没有该参数。它在调度时仍然返回一个成功的响应,只是不包括我需要的参数
下面是我在组件中映射分派的位置:
const mapDispatchToProps = {
filterUpdate,
getPosts
}
这里是实际调用它的方法
changeHandler (event) {
const checkedStatus = event.currentTarget.checked;
const selectedTaxonomy = event.currentTarget.name;
const termId = event.currentTarget.value;
const paramsToSend = {
checkedStatus: checkedStatus,
selectedTaxonomy: selectedTaxonomy,
termId: termId
};
const filterUpdatePromise = new Promise((resolve, reject) => {
this.props.filterUpdate(paramsToSend);
resolve(true);
});
// Run the filter query string format function only after the props have updated
filterUpdatePromise.then(() => {
const filterQs = this.formatNewFilterQuery();
getPosts(filterQs);
});
}
据我所知,getPosts(filterQs)正在执行中。如果我在操作中记录分派之外的filterq,我实际上会在日志中看到我的参数值。我就是不明白为什么不能将参数传递到调度中。尝试将您的
mapDispatchToProps
从
const mapDispatchToProps = {
filterUpdate,
getPosts
}
到
尝试从更改您的
mapDispatchToProps
const mapDispatchToProps = {
filterUpdate,
getPosts
}
到
我之前试过这个,但也没用。我只是再试了一次,但没有掷骰子。@BryanWhite,您是否能够创建一个最小的可复制示例,您可以将Redux添加到React项目中,并创建一个伪API调用。它使您的问题更容易解决。我不能,因为它依赖于wordpress自定义帖子类型和三个自定义分类法,它们只存在于本地,但不存在!我在github回购协议中也有这样的内容,如果这有帮助的话@BryanWhite,我刚才注意到的一件事是,当你调用
getPosts(filterQs)代码>,您没有通过执行this.props.getPosts(filterQs)通过调度程序调用函数代码>。但是,您可以在这里执行,this.props.filterUpdate(paramsToSend)代码>。你完全正确!我更新了代码,现在它工作了。谢谢你跟我说出来。我之前试过这个,但也没用。我只是再试了一次,但没有掷骰子。@BryanWhite,您是否能够创建一个最小的可复制示例,您可以将Redux添加到React项目中,并创建一个伪API调用。它使您的问题更容易解决。我不能,因为它依赖于wordpress自定义帖子类型和三个自定义分类法,它们只存在于本地,但不存在!我在github回购协议中也有这样的内容,如果这有帮助的话@BryanWhite,我刚才注意到的一件事是,当你调用getPosts(filterQs)代码>,您没有通过执行this.props.getPosts(filterQs)通过调度程序调用函数代码>。但是,您可以在这里执行,this.props.filterUpdate(paramsToSend)代码>。你完全正确!我更新了代码,现在它工作了。谢谢你跟我说出来。