Javascript 反应错误:对象作为反应子对象无效,如果要呈现子对象集合,请改用数组
我是react新手,当我使用react和flux进行crud应用时,我遇到了这个错误,我的代码如下 用户列表 商场 我不知道这里发生了什么问题。当我在stack over flow中检查时,我发现这是数据迭代的问题。但我已经做到了。即使错误仍然存在。如果有人能帮上忙,那将是非常可观的。Javascript 反应错误:对象作为反应子对象无效,如果要呈现子对象集合,请改用数组,javascript,html,reactjs,Javascript,Html,Reactjs,我是react新手,当我使用react和flux进行crud应用时,我遇到了这个错误,我的代码如下 用户列表 商场 我不知道这里发生了什么问题。当我在stack over flow中检查时,我发现这是数据迭代的问题。但我已经做到了。即使错误仍然存在。如果有人能帮上忙,那将是非常可观的。 setState方法中引发的错误。我在这里使用flux作为体系结构。当我尝试向数据添加新用户时会出现问题。实际上,当我将其添加到db时,不会出现任何问题。问题是,当我单击AddUser组件中的“保存”按钮时,它会
setState方法中引发的错误。我在这里使用flux作为体系结构。当我尝试向数据添加新用户时会出现问题。实际上,当我将其添加到db时,不会出现任何问题。问题是,当我单击AddUser组件中的“保存”按钮时,它会保存到我的数据库中,但在它需要再次显示为表列表后,会抛出错误。在React中迭代对象是很棘手的。通常,您希望使用
对象.条目
/对象.值
/对象.键
,然后使用映射
。这将获取对象并创建一个数组,然后您可以使用.map
遍历对象.map
对对象不起作用,因为它是数组函数,所以必须先将对象转换为数组。.map
函数将允许您在React中迭代,而不会出现该错误:
this.state = {
branch: "",
name: "",
username: "",
mobile: "",
email: "",
address: ""
}
// Not in your constructor
let myObj = this.state;
Object.entries(myObj).map(item => {
// Successfully iterates, do what you need to do in here
})
另外,将JQuery与React一起使用是一个非常糟糕的主意。React使用虚拟DOM,不能很好地与JQuery配合使用。在React中迭代对象是很棘手的。通常,您希望使用
对象.条目
/对象.值
/对象.键
,然后使用映射
。这将获取对象并创建一个数组,然后您可以使用.map
遍历对象.map
对对象不起作用,因为它是数组函数,所以必须先将对象转换为数组。.map
函数将允许您在React中迭代,而不会出现该错误:
this.state = {
branch: "",
name: "",
username: "",
mobile: "",
email: "",
address: ""
}
// Not in your constructor
let myObj = this.state;
Object.entries(myObj).map(item => {
// Successfully iterates, do what you need to do in here
})
另外,将JQuery与React一起使用是一个非常糟糕的主意。React使用虚拟DOM,不能很好地使用JQuery。我看到的一个问题是,您直接在JSX元素(
{user.dob}
中使用user.dob
)dob
是new Date()
或react datepicker
的onChange
事件的直接输出,两者都是对象
下面是一个试图呈现{new Date()}
的虚拟组件。
const-App=({text})=>{text};
//传递日期对象。
ReactDOM.render(,document.getElementById(“app”)代码>
我看到的一个问题是,您直接在JSX元素({user.dob}
中使用了user.dob
)dob
是new Date()
或react datepicker
的onChange
事件的直接输出,两者都是对象
下面是一个试图呈现{new Date()}
的虚拟组件。
const-App=({text})=>{text};
//传递日期对象。
ReactDOM.render(,document.getElementById(“app”)代码>
谢谢,伙计。这就是问题所在,伙计。这就是问题所在
import dispatcher from "../dispatcher/dispatcher";
import { BASE_URL } from "../utils/AppConstants";
export function getUsersList() {
console.log("getting the data! ");
fetch(BASE_URL + "/users")
.then(res => res.json())
.then(
result => {
console.log("res " + result);
dispatcher.dispatch({ type: "RECEIVE_USERS", users: result });
},
// Note: it's important to handle errors here instead of a catch() block so that
// we don't swallow exceptions from actual bugs in components.
error => {
// here manage error and close loading;
console.log("getting error " + error);
}
);
}
export function createNewUser(user) {
console.log("post the data!");
fetch(BASE_URL + "/saveuser", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(user)
})
.then(res => res.json())
.then(
result => {
dispatcher.dispatch({ type: "CREATE_USER", newUser: user });
},
// Note: it's important to handle errors here instead of a catch() block so that
// we don't swallow exceptions from actual bugs in components.
error => {
// here manage error and close loading;
console.log("getting error " + error);
}
);
}
import { EventEmitter } from "events";
import dispatcher from "../dispatcher/dispatcher";
class UserStore extends EventEmitter {
constructor() {
super();
dispatcher.register(this.handleActions.bind(this));
this.users = [
{
branch: "19",
name: "Javcbvcsim11",
username: "zxcv",
mobile: "5645654",
email: "demo@gmail.com111",
address: "Demo vcbvcbAddress1",
dob: "2020-11-06T00:00:00.000+0000"
}
];
}
createUser(newUser) {
this.users.push(newUser);
console.log("new users lenght " + this.users.lenght);
this.emit("change");
}
getAll() {
return this.users;
}
handleActions(action) {
switch (action.type) {
case "RECEIVE_USERS": {
this.users = action.users;
this.emit("change");
break;
}
case "CREATE_USER": {
this.createUser(action.newUser);
break;
}
}
}
}
export default new UserStore();
this.state = {
branch: "",
name: "",
username: "",
mobile: "",
email: "",
address: ""
}
// Not in your constructor
let myObj = this.state;
Object.entries(myObj).map(item => {
// Successfully iterates, do what you need to do in here
})