Skip to content

Instantly share code, notes, and snippets.

@3nvi
Created February 27, 2019 22:40
Show Gist options
  • Save 3nvi/0cfb93ac98932db2f60cbcd8c9ba07b4 to your computer and use it in GitHub Desktop.
Save 3nvi/0cfb93ac98932db2f60cbcd8c9ba07b4 to your computer and use it in GitHub Desktop.
Prefer CSS hiding instead of unmounting
// 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