Thursday, July 5, 2018

Reactjs - My Rough Notes

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

Note : Above answers and examples are understanding from my point of view and there are more than features which are not covered in each question.