I have a parent component named UsersTable
(it's a chile of some other component and has users
and roles
as its props). The getRoles()
function is getting all the roles for a user using an ajax request. The result is returned to render()
and stores in allroles
variable. allroles
is an array of Objects( [Object, Object, Object]
) and is sent to the child component, UserRow
, as its props. But I'm getting this error:
invariant.js:44 Uncaught Error: Objects are not valid as a React child
(found: object with keys {description, id, links, name}). If you meant to render a
collection of children, use an array instead or wrap the object using
createFragment(object) from the React add-ons. Check the render method of
`UserRow`.
Can someone please help me to fix it? Here is the parent component code:
export const UsersTable = React.createClass({
getRoles(){
var oneRole = "";
this.props.users.forEach(function(user){
server.getUserRoles(user.id,
(results) => {
this.oneRole =results['hits']['hits']
notifications.success("Get was successful ");
},
() => {
notifications.danger("get failed ");
});
}.bind(this));
return this.oneRole;
},
render() {
var rows = [];
var allroles = this.getRoles()
this.props.users.map(function(user) {
rows.push( <UserRow userID={user.id}
userEmail={user.email}
userRoles={allroles}
roles={this.props.roles} />);
}.bind(this));
return (
<table className="table">
<thead>
<tr>
<th>Email Address</th>
<th>Role</th>
<th>Edit</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
}
});
And here is the child component code:
export const UserRow = React.createClass({
render(){
return (
<tr>
<td>{this.props.userEmail}</td>
<td>{this.props.userRoles}</td>
</tr>
);
}
});
looks like the issue is when you render the userRoles. you need to loop over it and render an element for each role
export class UserRow extends React.Component {
render(){
return (
<tr>
<td>{this.props.userEmail}</td>
<td>{this.props.userRoles}</td>
--------------------^---------------------^
</tr>
);
}
};
try this
export class UserRow extends React.Component {
render(){
const roles = this.props.userRoles.map((role) => <div>{role.id || ''}</div>);
return (
<tr>
<td>{this.props.userEmail}</td>
<td>{roles}</td>
</tr>
);
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.