What is the difference between state and props in React?
State is referred to the local state of the component which cannot be accessed and modified outside of the component and only can be used & modified inside the component.
Plain JS
const DummyFunction = () => {
let name = 'Srinivas';
console.log(`Hey ${name}`)
}
React JS Equivalent
class DummyComponent extends React.Component {
state = {
name: 'Srinivas'
}
render() {
return
Hello
{this.state.name}
Props, on the other hand,make components reusable by giving components the ability to receive data from the parent component in the form of props.
Plain JS
const DummyFunction = (name) => {
console.log(`Hey ${name}`)
}
DummyFunction('Srinivas Narula');
DummyFunction('Santosh Narula');
React JS
class DummyComponent extends React.Component {
render() {
return
Hello {this.props.name}</div>;
}
}
// when using the component
<DummyComponent name="Srinivas Narula" />
<DummyComponent name="Santosh Narula"
When it is required/necessary to call super() inside constructor ?
Always call
super()
if you have a constructor and don't worry about it if you don't have a constructor
http://cheng.logdown.com/posts/2016/03/26/683329