Class Based Components in Older React
Class Based Components in Older React
Before React 16.8, we used to write components using both classes and functions.
But you could only declare state in class component and not in the functional
component.
You cannot use React hooks inside functional components. You cannot declare state
in functional components in older React.
Class component in old React is simply an ES6 class defined in modern JavaScript.
You'll have to extend (inherit) this class from Component class imported from the
react library.
This base class Component provides render() method inside this derived class. You
should use this method to return your JSX code which you would normally return
from a functional component.
This base class Component also provides access to other methods e.g. lifecycle
methods such as ComponentDidMount(), componentDidUpdate(),
componentWillUnmount(), componentWillReceiveProps() etc.
You should declare constructor() function and call super() method inside it so that it
calls the constructor() function of its base class.
State in class components could only be initialized in the constructor of the class.
Later it could be updated using the this.setState() function.
State in class components is always declared as an object. You should use this
keyword before state variable to point to the context of class.
this.state.showUsers;
State in class components is updated in a different way as compared to how it’s
updated in the functional components. State in class is not overridden completely;
only the relevant part is updated. Due to this reason, you can easily update the
relevant state slice without worrying about the other pieces of state. e.g.
this.setState({ showUsers: true });
If you want to update state based on the value of previous state, then you'll have to
pass function inside this.setState() function which will return a new state object.
this.setState(prevState => {
return {
showUsers: !prevState.showUsers,
};
})
You can declare extra functions e.g. click handler functions etc. inside the class. In
order to call them though, you'll have to access this before calling them and then
bind the value of this too to the function.
If you want to access props, you can access them inside lifecycle methods. They
are empty inside constructor function?
this keyword inside a class refers to the name of that class. Every function
declared inside the class is defined as a method on the this keyword.
While passing the reference of the function to an event handler (e.g.
click/change handler), you'll have to call bind on the function and pass the value
of this (which refers to the name of the class). If you don't do that, an uncaught
reference error will be thrown.
Unlike functional components, you can only use ONE context per class based
component.
You’ll have to declare a static property of the class and initialize it to the context
(imported from the store folder).
In order to access context variable now, you’ll use this.context.<variable-name>
You cannot use useContext() hook with class based components, that’s why we have
to declare static property and assign our context to it.