I need an explanation to React props like I am five
What is the meaning of {...this.props} in Reactjs - Stack Overflow
Serious: why are we passing data through props?
Should I learn using props or can I straight learn state?
Videos
Hey Guys,
I don't know why React props are so confusing. But wouldn't it make more sense if I just imported the component into what I intend to use it for rather than using props and creating a mess in App.js?
I was watching this guys video and countless others that do very similar things. They write in App.js and then use Props in their component to print the same code. It would make much more sense if they just imported.
TLDR: Kindly explain React Props, or send me a detailed link, some practices problems would help too
Thank You
It's called spread attributes and its aim is to make the passing of props easier.
Let us imagine that you have a component that accepts N number of properties. Passing these down can be tedious and unwieldy if the number grows.
<Component x={} y={} z={} />
So instead do this, wrap them up in an object and use the spread notation
var props = { x: 1, y: 1, z:1 };
<Component {...props} />
which will unpack it into the props on your component, i.e., you "never" use {... props} inside your render() function, only when you pass the props down to another component. Use your unpacked props as normal this.props.x.
It's ES6 Spread_operator and Destructuring_assignment.
<div {...this.props}>
Content Here
</div>
It's equal to Class Component
const person = {
name: "xgqfrms",
age: 23,
country: "China"
};
class TestDemo extends React.Component {
render() {
const {name, age, country} = {...this.props};
// const {name, age, country} = this.props;
return (
<div>
<h3> Person Information: </h3>
<ul>
<li>name={name}</li>
<li>age={age}</li>
<li>country={country}</li>
</ul>
</div>
);
}
}
ReactDOM.render(
<TestDemo {...person}/>
, mountNode
);

or Function component
const props = {
name: "xgqfrms",
age: 23,
country: "China"
};
const Test = (props) => {
return(
<div
name={props.name}
age={props.age}
country={props.country}>
Content Here
<ul>
<li>name={props.name}</li>
<li>age={props.age}</li>
<li>country={props.country}</li>
</ul>
</div>
);
};
ReactDOM.render(
<div>
<Test {...props}/>
<hr/>
<Test
name={props.name}
age={props.age}
country={props.country}
/>
</div>
, mountNode
);

refs
https://babeljs.io/docs/plugins/transform-object-rest-spread/
https://facebook.github.io/react/docs/components-and-props.html