Component vs Pure Component
Recently I came across something in React I oddly never saw before and this was a React.PureComponent. In our current system we use Redux as our global state manager and because of that we have moved to using component states to manage component level states using the this.setState({}) method.
interface AnimalProps {
species: string;
type: "omnivore" | "carnivore" | "herbivore"
}// PureComponent class example
export class Animal extends React.PureComponent <AnimalProps> {
constructor(props: AnimalProps) {
super(props);
}
}
At first I could not understand why we have React.Component and React.PureComponent until I saw it.
There is only one small difference between a Component and a PureComponent. A PureComponent manages the shouldComponentUpdate for you. PureComponents do what is called a shallow comparison for props and states where as a Component does not, and therefore a PureComponent with re-render whenever shouldComponentUpdate is triggered.