Javascript Redux返回未定义的承诺
我一直在努力想办法解决这个问题。目前,我所有的“绘画”道具都是未定义值的承诺: 在页面组件中:Javascript Redux返回未定义的承诺,javascript,reactjs,redux,frontend,prop,Javascript,Reactjs,Redux,Frontend,Prop,我一直在努力想办法解决这个问题。目前,我所有的“绘画”道具都是未定义值的承诺: 在页面组件中: componentDidMount = () => { this.props.setPaintingToProps(paintingId); } 在减速器中: case "SET_PAINTING": paintingService.getDetails(action.id).then(data=>{ return {...state, ...data} }
componentDidMount = () => {
this.props.setPaintingToProps(paintingId);
}
在减速器中:
case "SET_PAINTING":
paintingService.getDetails(action.id).then(data=>{
return {...state,
...data}
})
break;
reducer方法运行并且数据
正确,但在状态下它是Promise{:undefined}
提前谢谢你,如果有任何更多的信息需要解决这个问题,请询问 您应该为异步调用使用中间件,例如redux thunk来创建异步操作,然后分派数据
export const setPaintingToProps = (paintingId) =>
(dispatch) => {
paintingService.getDetails(paintingId).then(data => {
dispatch({ type: "SET_PAINTING", payload: data });
})
};
const mapDispatchToProps = dispatch => {
return {
setPaintingToProps: paintingId => dispatch(setPaintingToProps(paintingId));
}
};
};
case "SET_PAINTING":
return {...state,
action.payload}
})
break;
让我知道它是否对您有效,然后(data=>{return}将返回回调函数。您需要在
paintingService.getDetails()
i:ereturn paintingService.getDetails()
之前添加return。是的,这部分不是我最近的尝试。现在看起来像这样,但它仍然是一个已解决的承诺(使用正确的数据)。setPaintingToProps:paintingId=>{return paintingService.getDetails(paintingId)。然后(Saint=>{dispatch(actions.setPaintingToProps(Saint));});}setPaintingToProps操作抛出错误“操作必须是普通对象。使用自定义中间件执行异步操作。”不过,我安装了中间件:const store=createStore(paintingsReducer、applyMiddleware(thunk)和&window.\uuuu REDUX\u DEVTOOLS\uuu EXTENSION\uuuuuux\u DEVTOOLS\uu EXTENSION&&window.\uuuuuuuu REDUX\u DEVTOOLS\u EXTENSION());你确定你的SetPaintingTopops操作返回一个函数吗?我必须进一步研究Thunk,因为我还不能通过那个错误。我的最后一次尝试:const SetPaintingTopops=paintingId=>{paintingService.getDetails(paintingId)。然后(data=>{return type:“SET_PAINTING”,有效载荷:data};})};const-mapDispatchToProps=dispatch=>{return{setPaintingToProps:paintingId=>dispatch(setPaintingToProps(paintingId))};
您安装了什么中间件?我安装了thunk,createStore(reducer,applyMiddleware(thunk))
,但仍然有错误“操作必须是普通对象”当我尝试返回函数时
export const setPaintingToProps = (paintingId) =>
(dispatch) => {
paintingService.getDetails(paintingId).then(data => {
dispatch({ type: "SET_PAINTING", payload: data });
})
};
const mapDispatchToProps = dispatch => {
return {
setPaintingToProps: paintingId => dispatch(setPaintingToProps(paintingId));
}
};
};
case "SET_PAINTING":
return {...state,
action.payload}
})
break;