1
votes

We create components in react by extending React.component.I was reading code that in constructor of component we call super(props) and in parent constructor it sets props on our component instance.Apart from instantiating our component instance what else functionality React.component provides when we extends it.I also read if we do not extend react.component we cannot have state of our component.how is it related with state of component?

1
You can see the implementation of React.Component here: github.com/facebook/react/blob/master/packages/react/src/…. it's only a couple dozen lines of codeNicholas Tower
yes,I checked it .Does 'this' used in React.component function refer to component instance?and also it is function ..we are extending functionSuraj Prakash Joshi
Yes, this refers to the component instance. this behaves exactly the same in a react component as it does anywhere else in javascript. also it is function ..we are extending function Yes, all classes in javascript are functions. The class keyword is just syntactic sugar on top of functions and prototypes.Nicholas Tower

1 Answers

0
votes

When you extend then React.Component it allows you to do different tasks:

  • Allows to initialize the props
  • Allows to initialize the states
  • Allows to use lifecycle hooks
  • Allows to use custom hooks
  • Allows to do anything that allows a Class to do