and Lifecycles
As opposed to functional components, class components are classes that
Why class components?
Example
function Counter() {
return (
<div>
<h1>0</h1>
<button>Inc</button>
</div>
);
}
<Counter />
class Counter extends React.Component {
render() {
return (
<div>
<h1>0</h1>
<button>Inc</button>
</div>
);
}
}
<Counter />
Example
function Counter(props) {
const { startFrom } = props;
return (
<div>
<h1>{startFrom}</h1>
<button>Inc</button>
</div>
);
}
<Counter startFrom={32} />
class Counter extends React.Component {
render() {
const { startFrom } = this.props;
return (
<div>
<h1>{startFrom}</h1>
<button>Inc</button>
</div>
);
}
}
<Counter startFrom={32} />
Example
function Counter(props) {
const [num, setNum] =
useState(props.startFrom)
return (
<div>
<h1>{num}</h1>
<button>Inc</button>
</div>
);
}
<Counter startFrom={32} />
class Counter extends React.Component {
state = {
num: 0
};
render() {
return (
<div>
<h1>{this.state.num}</h1>
<button>Inc</button>
</div>
);
}
}
<Counter startFrom={32} />
Example
function Counter(props) {
const [num, setNum] =
useState(props.startFrom);
return (
<div>
<h1>{num}</h1>
<button
onClick={function () {
setNum(num + 1);
}}
>
Inc
</button>
</div>
);
}
<Counter startFrom={32} />
class Counter extends React.Component {
state = {
num: this.props.startFrom
};
handleClick = () => {
this.setState({ num: this.state.num + 1 });
};
render() {
return (
<div>
<h1>{this.state.num}</h1>
<button
onClick={this.handleClick}
>
Inc
</button>
</div>
);
}
}
<Counter startFrom={32} />
functional | class | |
---|---|---|
props | as argument to the fn | on the `this` with key `props` |
inherit | nothing | React.Component |
type | function | class |
state | useState | `this.state` in the constructor |
update state | using the 2nd function returned from useState | using `this.setState` in a function / method |
In short
Mounting
Avoid
These methods are called in the following order when an instance of a component is being created and inserted into the DOM:
constructor
Typically, in React constructors are only used for two purposes:
You should not call setState() in the constructor().
render()
The render() method is the only required method in a class component.
The render() function should be pure, meaning that it does not modify component state.
componentDidMount()
Updating
An update can be caused by changes to props or state. These methods are called in the following order when a component is being re-rendered:
Avoid (they are old lifecycle methods)
componentDidUpdate(prevProps, prevState, snapshot)
It is invoked immediately after updating occurs. This method is not called for the initial render.
You can use this method to do side-effects based on prop values. Just like componentDidMount
This method is called when a component is being removed from the DOM:
componentWillUnmount()
componentWillUnmount() is invoked immediately before a component is unmounted and destroyed.
Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in componentDidMount().
Once a component instance is unmounted, it will never be mounted again.