Skip to content

Instantly share code, notes, and snippets.

@KobbyMmo
Last active January 30, 2022 05:56
Show Gist options
  • Save KobbyMmo/2ba4f2da065e0a264f3d729c3752b4dd to your computer and use it in GitHub Desktop.
Save KobbyMmo/2ba4f2da065e0a264f3d729c3752b4dd to your computer and use it in GitHub Desktop.
Avoid Unnecessary renders in React
function Info({ radius }) {
<div>
<p>Radius:{radius}</p>
<p>Area:{(radius * radius * 22) / 7}</p>
</div>;
}
const MemoizedInfo = React.memo(Info);
function Circle({ radius, xCenter, yCenter }) {
<svg height="100%" width="100%">
<circle cx={xCenter} cy={yCenter} r={radius} />
</svg>;
}
const MemoizedCircle = React.memo(Circle);
function Screen({ radius, xCenter, yCenter }) {
<>
<MemoizedCircle radius={radius} xCenter={xCenter} yCenter={yCenter} />
<MemoizedInfo radius={radius} />
</>;
}
const MemoizedScreen = React.memo(Screen);
function Screen({ radius, xCenter, yCenter }) {
const onHandleClick = useCallback(() => {
superClick(radius)
});
<>
<MemoizedCircle radius={radius} xCenter={xCenter} yCenter={yCenter} />
<MemoizedInfo radius={radius} />
<MemoizedButton handleClick={onHandleClick}/>
</>;
}
function Button(handleClick) {
<button onClick={handleClick}>Click me</button>;
}
const MemoizedButton = React.memo(Button)
function Screen({ radius, xCenter, yCenter }) {
<>
<MemoizedCircle radius={radius} xCenter={xCenter} yCenter={yCenter} />
<MemoizedInfo radius={radius} />
<MemoizedButton handleClick={() => {superClick(radius)}}/>
</>;
}
function Circle({ radius, xCenter, yCenter }) {
 <svg height="100%" width="100%">
 <circle cx={xCenter} cy={yCenter} r={radius} />
 </svg>;
}
function Info({ radius }) {
 <div>
 <p>Radius:{radius}</p>
 <p>Area:{(radius * radius * 22) / 7}</p>
 </div>;
}
function Screen(props) {
 <>
 <Circle {…props} />
 <Info {…props} />
 </>;
}
function Screen({ radius, xCenter, yCenter }) {
<>
<Circle radius={radius} xCenter={xCenter} yCenter={yCenter} />
<Info radius={radius} />
</>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment