javascript - 将对象数组作为 React 组件的 Prop

标签 javascript ajax reactjs

我有一个名为 UsersTable 的父组件(它是某个其他组件的子集,并且有 usersroles 作为它的属性)。 getRoles() 函数使用 ajax 请求获取用户的所有 Angular 色。结果返回到 render() 并存储在 allroles 变量中。 allroles 是一个 Objects([Object, Object, Object]) 数组,作为其属性被发送到子组件 UserRow。但是我收到了这个错误:

    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`.

有人可以帮我解决吗?这是父组件代码:

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>
        );
    }    
});

这是子组件代码:

export const UserRow = React.createClass({
    render(){
        return (
            <tr>
                <td>{this.props.userEmail}</td>
                <td>{this.props.userRoles}</td>
            </tr>
        );
    }
});

最佳答案

看起来问题出在您呈现 userRoles 时。您需要遍历它并为每个 Angular 色呈现一个元素

export class UserRow extends React.Component {
    render(){
        return (
            <tr>
                <td>{this.props.userEmail}</td>
                <td>{this.props.userRoles}</td>
--------------------^---------------------^
            </tr>
        );
    }
};

试试这个

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>
        );
    }
};

关于javascript - 将对象数组作为 React 组件的 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42385394/

相关文章:

javascript - 在 Angular 谷歌地图中显示集群中的标记数量

javascript - 如果选择选项做某事

jquery - Jquery 的 Ajax 自动完成 : How To Send Dynamic Parameters

reactjs - 为什么 ReactJS 中 DOM 不随着状态变化而更新?

javascript - 将js数组分为2个部分

javascript - 匹配文本中的多个关键字(Javascript)

javascript - 在另一个字符串的位置追加新字符串

javascript - 从列表项中获取 ID 以使用 ajax 更新 div

javascript - jQuery 的 `getScript` 失败。路径正确,脚本下载正确。

javascript - 从 Promise : [Object Object] 返回一个值