javascript - ReactJs 无法访问 props

标签 javascript reactjs

我正在尝试访问子组件中的 props,我正在使用映射的 json 结果渲染网格行:

getRowNodes: function() {
    return this.props.contacts.map(function(contact){
        return <Row 
                key={contact.id} 
                contact={contact} 
                columns={this.props.children} />;
    }.bind(this));
}

当我渲染组件时,我可以控制台日志 {this.props.data} 并查看所有属性,我还可以在 chrome 开发工具中查看所有属性,但是,当我尝试访问属性 this.props 时。 data.propertyName 我未定义。

如果我尝试访问下面的任何属性,我会收到错误......有什么想法吗?

enter image description here

最佳答案

就像Kirill Slatin所说:你必须把它包起来。

试试这个:

getRowNodes() {
    return (
        <div>
            {this.props.contacts.map(this._getRow)}
        </div>
    );
},

_getRow(contact) {
    return (
        <Row 
            key={contact.id}
            contact={contact}
            columns={this.props.children} />
    );
}

NOTE: I have optimized the readability by using JSX Syntax.

关于javascript - ReactJs 无法访问 props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31132170/

相关文章:

javascript - CSS : Justify dynamically created divs with variable width inside parent

Javascript 调试困难,因为浏览器不刷新脚本!

javascript - 如何从 localStorage 读取 DraftJS 状态?

javascript - 在异步函数中调用函数(渲染组件)

javascript - 我的 CSS 样式表对我的 React 组件没有影响

javascript - 允许 Material UI RaisingButton 标签中存在变量

javascript - 我的 HTML 按钮的 javascript 不会第二次切换

javascript - 将鼠标悬停在列表项上,只显示一项而不是全部

javascript - 在这种情况下,在 AngularJS 中处理事件的最佳方式是什么?

javascript - 将鼠标悬停在嵌套的 React 组件上会触发父组件 onMouseOut 和无限循环