javascript - 获得不变违规:对象作为 React 子对象无效
问题描述
我的代码不起作用,它总是返回
Invariant Violation:对象作为 React 子对象无效(发现:具有键 {_40、_65、_55、_72} 的对象)。如果您打算渲染一组子项,请改用数组。
那里有任何错误的语法或逻辑吗?这是我的代码:
const asyncTest1= async() => {
try {
noteAction({ type: SET_LOADING, payload: true });
const response = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Async Test Load');
}, 3000);
});
const adding = noteAction({ type: ADD_NOTE, payload: response });
const setLoadingFalse = noteAction({ type: SET_LOADING, payload: false });
const result = await Promise.all([response, adding, setLoadingFalse]);
return result;
} catch (e) {
console.log(e);
}
};
但没有 async/await 版本,我的代码正在运行:
const asyncTest2= () => {
try {
noteAction({ type: SET_LOADING, payload: true });
const result = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Async Test Load');
}, 3000);
});
return result
.then(response => noteAction({ type: ADD_NOTE, payload: response }))
.then(response => noteAction({ type: SET_LOADING, payload: false }));
} catch (e) {
console.log(e);
}
};
解决方案
但没有异步/等待版本,我的代码正在运行
async
与/语法的等价物await
不会使用 any Promise.all
,它会是
const asyncTest2 = () => {
try {
noteAction({ type: SET_LOADING, payload: true });
} catch (e) {
console.log(e);
}
var result = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Async Test Load');
}, 3000);
});
var response = await result;
var response = await noteAction({ type: ADD_NOTE, payload: response }));
return noteAction({ type: SET_LOADING, payload: false }));
};
我不知道这是否是您真正想要的,但至少它与then
版本相同。
推荐阅读
- sequelize.js - 如何在 sequelize 中深度嵌套数据?
- c++ - wxWidgets - 单击按钮时隐藏/显示 `boxsizer` 的内容
- python - 使 Python 模块可供所有 Linux 用户使用
- reactjs - Typescript 4.0 中的 SCSS 接口
- lua - 检查球与砖块的碰撞并将砖块区域撤消到空白空间并反转x速度
- scala - 通过在 spark/scala 中对键进行分组后需要一些数据
- c++ - C++,为什么迭代器上的 ++ 运算符不起作用,如果放在表达式之后,*(v.begin() + i / 10)++?
- python - 使用 python 脚本获取 Instagram 关注者列表
- r - 用两个条件过滤一个小标题
- sql-server - 使用他们的数据库选项卡、SSMS、My Little Admin 或其他方式在 Plesk 中填充我的数据库的正确方法是什么?