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;