I'm new to React, I just followed a tutorial step by step and got other results. The error message I'm getting is:
Cannot read property 'name' of undefined.
The question is why is name undefined? I added objects that I need to show on a page a list.
Here is my code where I fall:
import React, { Component } from "react";
class UserItem extends Component {
render() {
return (
<li className="UserLI">
{this.props.users.name} is {this.props.users.age} years old.
</li>
);
}
}
export default UserItem;
And here is where I call the above code:
class Users extends Component {
render() {
let userItem;
if (this.props.users) {
userItem = this.props.users.map(user => {
console.log(user);
return <UserItem key={user.id} user={user} />;
});
}
return <div classNname="Users">{userItem}</div>;
}
}
my JSON data
this.setState({users:[
{
id:0,
name: "karam",
age:22
},
{
id:1,
name: "ayoub",
age:23
},
{
id:2,
name: "tarek",
age:21
}
]});
I'm new to React, I just followed a tutorial step by step and got other results. The error message I'm getting is:
Cannot read property 'name' of undefined.
The question is why is name undefined? I added objects that I need to show on a page a list.
Here is my code where I fall:
import React, { Component } from "react";
class UserItem extends Component {
render() {
return (
<li className="UserLI">
{this.props.users.name} is {this.props.users.age} years old.
</li>
);
}
}
export default UserItem;
And here is where I call the above code:
class Users extends Component {
render() {
let userItem;
if (this.props.users) {
userItem = this.props.users.map(user => {
console.log(user);
return <UserItem key={user.id} user={user} />;
});
}
return <div classNname="Users">{userItem}</div>;
}
}
my JSON data
this.setState({users:[
{
id:0,
name: "karam",
age:22
},
{
id:1,
name: "ayoub",
age:23
},
{
id:2,
name: "tarek",
age:21
}
]});
Share
Improve this question
edited Nov 26, 2018 at 9:22
Roy Scheffers
3,90811 gold badges33 silver badges36 bronze badges
asked May 7, 2018 at 17:16
Karam HajKaram Haj
1,2602 gold badges11 silver badges20 bronze badges
2
- 1 {this.props.users.name} is {this.props.users.age} years old. should be {this.props.user.name} is {this.props.user.age} years old. – Suthan Bala Commented May 7, 2018 at 17:19
-
You're passing
user
, notusers
, to theUserItem
ponent. – Lennholm Commented May 7, 2018 at 17:21
3 Answers
Reset to default 2The issue is your data not ing at the moment the code get render. you can fix that issue by adding basic validation.
class UserItem extends Component {
render() {
return (
<li className="UserLI">
{this.props.users && this.props.users.name} is {this.props.users && this.props.users.age} years old.
</li>
);
}
}
You pass a user
prop:
<UserItem key={user.id} user={user} />
But you try to access it as users
<li className="UserLI">
{this.props.users.name} is {this.props.users.age} years old.
</li>
It's a typo. Fix:
<li className="UserLI">
{this.props.user.name} is {this.props.user.age} years old.
</li>
You need to access name
and age
from this.props.user
and not this.props.users
, since you are passing the data as prop user
class UserItem extends Component {
render() {
return(
<li className="UserLI">
{this.props.user.name} is {this.props.user.age} years old.
</li>
)
}
}
export default UserItem;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744910610a4600526.html
评论列表(0条)