Created
February 27, 2019 22:40
-
-
Save 3nvi/0cfb93ac98932db2f60cbcd8c9ba07b4 to your computer and use it in GitHub Desktop.
Prefer CSS hiding instead of unmounting
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
// Avoid this is the components are too "heavy" to mount/unmount | |
function Component(props) { | |
const [view, setView] = useState('view1'); | |
return view === 'view1' ? <SomeComponent /> : <AnotherComponent /> | |
} | |
// Do this instead if you' re opting for speed & performance gains | |
const visibleStyles = { opacity: 1 }; | |
const hiddenStyles = { opacity: 0 }; | |
function Component(props) { | |
const [view, setView] = useState('view1'); | |
return ( | |
<React.Fragment> | |
<SomeComponent style={view === 'view1' ? visibleStyles : hiddenStyles}> | |
<AnotherComponent style={view !== 'view1' ? visibleStyles : hiddenStyles}> | |
</React.Fragment> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment