admin管理员组文章数量:1320661
I'm using reactjs to populate state.users with a new member when he or she signs in.
The following code is working
var users = this.state.users;
users.push(member);
this.setState({users: users});
But when I try to wrap everything in one line of code it fails
this.setState({users: this.users.state.push(member)});
When I try the above I get an error further down in my list.jsx
Uncaught TypeError: this.props.users.map is not a function
Any idea what is causing this? I'm thinking that when I use the one line solution the wrong kind of object is passed into the state?
Thank you!
I'm using reactjs to populate state.users with a new member when he or she signs in.
The following code is working
var users = this.state.users;
users.push(member);
this.setState({users: users});
But when I try to wrap everything in one line of code it fails
this.setState({users: this.users.state.push(member)});
When I try the above I get an error further down in my list.jsx
Uncaught TypeError: this.props.users.map is not a function
Any idea what is causing this? I'm thinking that when I use the one line solution the wrong kind of object is passed into the state?
Thank you!
Share Improve this question asked Aug 5, 2015 at 10:50 Miguel StevensMiguel Stevens 9,23019 gold badges75 silver badges135 bronze badges1 Answer
Reset to default 5It happens because .push
returns the new length (int number), not array,
var data = {
users: []
};
data.users = data.users.push(10);
console.log(typeof data.users); // number
.push
- return the new length property of the object upon which the method was called.
You can use .concat
instead of .push
this.setState({
users: this.state.users.concat(member)
});
or
this.setState({
users: [...this.state.users, member]
});
本文标签: javascriptReactjs adding an object to stateStack Overflow
版权声明:本文标题:javascript - Reactjs adding an object to state - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742064429a2418747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论