简体   繁体   English

This.props 返回未定义?

[英]This.props returning undefined?

I'm currently passing data into my component via props, and for some reason it's showing up as undefined.我目前正在通过 props 将数据传递到我的组件中,但出于某种原因,它显示为未定义。 From my parent component perspective I have the following pass 2 props, data and header .从我的父组件的角度来看,我有以下 pass 2 props, dataheader

class ContractTable extends Component {
constructor(props) {
    super(props);
    this.state = {
        data: []
    };
}

render() {
    return (
        <div>
            <p></p>
            <MuiThemeProvider>
                <TableTest
                    data={this.state.data}
                    header={[
                        {
                            name: "First Name",
                            prop: "firstName"
                        },
                        {
                            name: "Last Name",
                            prop: "lastName"
                        },
                        {
                            name: "Age",
                            prop: "age"
                        },
                        {
                            name: "Height",
                            prop: "height"
                        },
                        {
                            name: "Address",
                            prop: "address"
                        }
                    ]}
                />
            </MuiThemeProvider>
            <p></p>
        </div>
    );
}

I try to grab the props and set it as my state, but when I log this.props.data or this.props.header it returns undefined.我尝试获取道具并将其设置为我的状态,但是当我记录this.props.datathis.props.header它返回未定义。 Why is this?为什么是这样?

import React, { Component } from 'react';
import {
Table,
TableBody,
TableHeader,
TableHeaderColumn,
TableRow,
TableRowColumn,
} from 'material-ui/Table';

class TableTest extends Component {
constructor(props) {
    super(props);

    this.state = {
        data: props.data,
        header: props.header
    }

    this.row = this.row.bind(this);
}

row = (currentValue, index, header) => (
    <TableRow key={`t-${index}`}>
        {
            header.map((headerName, index) => (
                <TableRowColumn key={`trc-${index}`}>
                    {currentValue[headerName.prop]}
                </TableRowColumn>
            ))
        }
    </TableRow>
);

render() {
    return 'hello'
}
}

export default TableTest;

Update: take a look https://jsfiddle.net/nroLmghv/更新:看看https://jsfiddle.net/nroLmghv/

Just rendered simple table header.只是呈现简单的表头。

Passing props to state is not a good approach.将 props 传递给 state 不是一个好方法。


I created a snippet.我创建了一个片段。 And it looks working.它看起来有效。 Point in which place do you have a problem.点哪个地方有问题。 Or provide MuiThemeProvider and TableTest full code.或者提供MuiThemeProviderTableTest完整代码。

 class Example extends React.Component { constructor(props) { super(props) this.state = { // mock value data: "some value" } } render() { return <div> <TableTest data={this.state.data} header={[ { name: "First Name", prop: "firstName" }, { name: "Last Name", prop: "lastName" }, { name: "Age", prop: "age" }, { name: "Height", prop: "height" }, { name: "Address", prop: "address" } ]} /> </div>; } } class TableTest extends React.Component { constructor(props) { super(props); this.state = { data: this.props.data, header: this.props.header } console.log(this.state.data) console.log(this.state.header) } render() { return <div></div>; } } ReactDOM.render( <Example />, document.getElementById('root') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"> </div>

Its an antipattern to set a state that is directly derivable from props, you would rather use the props directly.它是一种设置可直接从 props 派生的状态的反模式,您宁愿直接使用 props。 Also if you use the props to set state and you need to update state based on props change, you would also need to implement componentWillReceiveProps function此外,如果您使用道具设置状态并且需要根据道具更改更新状态,则还需要实现componentWillReceiveProps功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM