Last active
January 30, 2022 05:56
-
-
Save KobbyMmo/2ba4f2da065e0a264f3d729c3752b4dd to your computer and use it in GitHub Desktop.
Avoid Unnecessary renders in React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Screen({ radius, xCenter, yCenter }) { | |
<> | |
<MemoizedCircle radius={radius} xCenter={xCenter} yCenter={yCenter} /> | |
<MemoizedInfo radius={radius} /> | |
</>; | |
} | |
const MemoizedScreen = React.memo(Screen); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Screen({ radius, xCenter, yCenter }) { | |
const onHandleClick = useCallback(() => { | |
superClick(radius) | |
}); | |
<> | |
<MemoizedCircle radius={radius} xCenter={xCenter} yCenter={yCenter} /> | |
<MemoizedInfo radius={radius} /> | |
<MemoizedButton handleClick={onHandleClick}/> | |
</>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | |
</>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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