Props pass to constructor of a class is for ? Since I can get my props using this.props.something
, why I still seeing some people pass the props to the constructor? more confusingly it's passed to the 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')
);
<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>