Hope the question is clear enough (sorry, but english is not my first language).
I will write an example.
Consider two components Product
and ProductList
class ProductList extends React.Component {
render() {
data = {title:"Iphone", description: "The most sold smartphone"};
return (
<div className='productList'>
<Product title={data.title} description={data.description} />
</div>
);
}
}
class Product extends React.Component {
render() {
return (
<div className='item'>
<div className='description'>
<a>{this.props.title}</a>
<p>{this.props.description}</p>
</div>
</div>
);
}
}
ReactDOM.render( <ProductList />, document.getElementById('content') );
This kind of example is very common in tutorial and guides about React. It shows a parent component and a child component, with the parent component containing some data (for simplicity they are just store in an object data
) and a child component receiving data from the parent component.
I just notice that data usefull only to a child component are often placed inside the parent component and then passed down through the props. Why in most of the React examples data are placed in the parent/root component and then passed down with props? Wouldn't it be easier to place data directly in the child component avoiding to use props?