从另一个组件中获取道具值

时间:2017-01-09 12:59:41

标签: javascript reactjs

道具传递给一个类的构造函数是为了什么?既然我可以使用this.props.something获取道具,为什么我仍然看到有些人将道具传递给构造函数?更令人困惑的是,它传递给了super(props)



class Todo extends React.Component {
  constructor(){
    super()
    this.data = ['write book','wash clothes','jogging'];
  }
  render() {
    return (      
      <div>
        <ul>
         {this.data.map((item)=><TodoItem key={item} item={item}/>)}
        </ul>
      </div>
    );
  }
}

class TodoItem extends React.Component {
  constructor(props){
    super(props); // what is this for here?
  }
  render(){
    return(

      <li>
        <span>{this.props.item}</span>
      </li>
    )
  }
}

ReactDOM.render(
  <Todo/>,
  document.getElementById('react_example')
);
&#13;
<div id="react_example"></div><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>
&#13;
&#13;
&#13;

0 个答案:

没有答案