javascript - Redux 返回未定义的承诺
问题描述
我一直试图弄清楚 Redux。目前我所有的“绘画”道具都是一个未定义价值的承诺:
在页面组件中:
componentDidMount = () => {
this.props.setPaintingToProps(paintingId);
}
...
const mapStateToProps = state => {
return {
painting: state
};
};
const mapDispatchToProps = dispatch => {
return {
setPaintingToProps: paintingId => {
dispatch({ type: "SET_PAINTING", id: paintingId });
}
};
};
在减速机中:
case "SET_PAINTING":
paintingService.getDetails(action.id).then(data=>{
return {...state,
...data}
})
break;
reducer 方法运行并且data
是正确的,但在状态下它是Promise {<resolved>: 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;
请让我知道这对你有没有用
推荐阅读
- python - 为什么我有 python 错误 unindent does not match any external indentation level 错误
- android - android:kotlin:实现分页3时数据未加载到适配器中
- haskell - 给定一个列表,如何仅对每两个元素满足二元谓词的子列表执行一些转换?
- c# - 创建自定义可序列化值类型
- sql-server - 如何使用 SQL Server 从数据库中获取具有列的表的特定列中的所有表名和不同值
- palantir-foundry - 为什么我不应该在我的 Python 转换中使用 collect()?
- spring-boot - 在不启动服务器的情况下使用 Spring 应用程序属性
- apache-nifi - 如何在 NiFi 处理器中重新加载外部配置数据
- php - 上传图片或创建新帖子时,Wordpress 中的 net::ERR_CONNECTION_RESET
- cllocationmanager - iOS 14 - 当精确位置权限被拒绝时,如何让 iBeacon 唤醒应用程序?