在redux中传递参数
我想在redux中传递一个参数,但我没有找到方法 这是我的密码: 重演/行动在redux中传递参数,redux,react-redux,Redux,React Redux,我想在redux中传递一个参数,但我没有找到方法 这是我的密码: 重演/行动 export const next_page=(max_page)=>{ 返回{ 类型:下一页, 有效载荷:最大页面 } }您只需通过mapDispatchToProps中的下一页道具即可: const mapDispatchToProps = (dispatch) => ({ nextPage: max_page => dispatch(next_page(max_page)), }) 然后,当您
export const next_page=(max_page)=>{
返回{
类型:下一页,
有效载荷:最大页面
}
}
您只需通过mapDispatchToProps
中的下一页道具即可:
const mapDispatchToProps = (dispatch) => ({
nextPage: max_page => dispatch(next_page(max_page)),
})
然后,当您在ChangePage
组件中调用它时,您的max\u page
参数将已经被传递
此外,请注意,您不需要中间件来再次重新分派操作(这将导致无限循环):
并且您的减速器应该打开动作。键入,而不是动作。有效载荷:
// redux/reducer
export default (page = 1, action) => {
switch (action.type) { // changed from action.payload
case NEXT_PAGE:
const nextPage = page + 1
return nextPage <= action.payload ? nextPage : page
default:
return page
}
}
//redux/reducer
导出默认值(第1页,操作)=>{
开关(action.type){//已从action.payload更改
案例下页:
const nextPage=第+1页
返回下一页
// redux/reducer
export default (page = 1, action) => {
switch (action.type) { // changed from action.payload
case NEXT_PAGE:
const nextPage = page + 1
return nextPage <= action.payload ? nextPage : page
default:
return page
}
}