【问题标题】:ReactJs cannot access propsReactJs 无法访问 props
【发布时间】:2015-06-30 07:30:40
【问题描述】:

我正在尝试访问子组件中的道具,我正在使用映射的 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 我没有定义。

如果我尝试访问以下任何属性,我会收到错误消息。有什么想法吗?

【问题讨论】:

  • 文档说 React.js 只能渲染单个节点。如果有多个节点,则应将它们包装到一个根中

标签: javascript reactjs


【解决方案1】:

就像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} />
    );
}

注意:我已经使用 JSX 语法优化了可读性。

【讨论】:

    猜你喜欢
    • 2018-10-24
    • 2019-01-10
    • 2015-05-25
    • 1970-01-01
    • 2018-11-06
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    相关资源
    最近更新 更多