Class Components
In React class components, we are told that setState always causes a re-render, regardless of whether
This is not a direct answer to the OP, but related and maybe helpful for some people new to React and/or Hooks and struggling with their side-effect and render timing.
Since it hasn't been mentioned here yet: In functional components rather than using the beforementioned (see comments of the accepted answer) ShouldComponentUpdate() function, which is for class-based Components only, you would use the useEffect() hook. With it you can tell your component when to run the side effects AND under which condition, e.g. when certain dependencies have changed.
In this example from the React docs, only when props.source changed, the function will be executed.
useEffect(
() => {
const subscription = props.source.subscribe();
return () => {
subscription.unsubscribe();
};
},
[props.source],
);
React docs: useEffect()