javascript - 为什么在 react-redux 上未定义 [object Object],[object Object]?
问题描述
我有一个:
classesAll:[0:{id:1,title:'test1'},1:{id:2,title:'test2'}]
当我将它“console.log(this.props.classes.classesAll)”到控制台时,它不会返回我的数组的异常值。相反,它显示以下内容: undefined [object Object],[object Object]
async componentDidMount() {
await this.props.showAllClasses();
console.log("***", this.props.classes.classesAll)
}
Redux action:
export const showAllClasses = () => {
return async dispatch => {
dispatch(pageLoading(true));
await Api.get(`classes.php`).then(res => {
//this.state.all = res.data;
const classesAll = res.data.data;
state.classesAll = classesAll;
});
await dispatch(onChangeClasessAll(state.classesAll));
dispatch(pageLoading(false));
};
};
export const onChangeClasessAll = classesAll => {
const type = CLASSES_ALL;
return { type , classesAll};
};
解决方案
因为您的请求是异步的。您应该在请求返回时对其进行控制台。在 react-native 0.6 中,您可以在 getDerivedStateFromProps(props, state) 方法中执行此操作
static getDerivedStateFromProps(props, state) {
console.log(props.classes.classesAll)
}
在 react-native 0.6 之前,你应该在 componentWillReceiveProps() 方法中进行
componentWillReceiveProps(nextProps) {
console.log(props.classes.classesAll)
}
推荐阅读
- python - 如何将具有相同 ID 的一列与 Pandas 中的其余列连接?
- python - 多次执行“finish_bundle”方法:Apache beam、Google Dataflow
- mysql - 如何将值插入到帖子和标签的多:多关系表中?
- graphviz - 安装 Graphviz 后无法执行 dot 命令。(“点”没有布局引擎支持)
- flutter - Flutter FirebaseAnimatedList Assertion Error on Push in sorted list
- three.js - Threejs的网格中的单击事件
- python - python/django的ckfinder
- c# - 如何在结果数可能不同的结果视图中访问字符串值?
- android - 如何使用 Motion Layout 在 Android 中实现折叠标题?
- laravel - Laravel snappy PDF - 退出状态码