Last active
February 28, 2019 08:28
-
-
Save 3nvi/537af1c6eed1031af19b289d301fafa3 to your computer and use it in GitHub Desktop.
Utilization of bail-out techniques
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
// index.jsx | |
export default function ParentComponent(props) { | |
return ( | |
<div> | |
<SomeComponent someProp={props.somePropValue} | |
<div> | |
<AnotherComponent someOtherProp={props.someOtherPropValue} /> | |
</div> | |
</div> | |
) | |
} | |
// ./SomeComponent.jsx | |
export default function SomeComponent(props) { | |
return ( | |
<div>{props.someProp}</div> | |
) | |
} | |
// -------------------------------------------- | |
// ./AnotherComponent.jsx (1st variation) | |
// This component will render anytime the value of `props.somePropValue` changed | |
// regardless of whether the value of `props.someOtherPropValue` has changed | |
export default function AnotherComponent(props) { | |
return ( | |
<div>{props.someOtherProp}</div> | |
) | |
} | |
// ./AnotherComponent.jsx (2nd variation) | |
// This component will render only render when its *own* props have changed | |
export default memo((props) => { | |
return ( | |
<div>{props.someOtherProp}</div> | |
) | |
}); | |
// ./AnotherComponent.jsx (3rd variation) | |
// This component will also render only render when its *own* props have changed | |
class AnotherComponent extends React.PureComponent { | |
render() { | |
return <div>{this.props.someOtherProp}</div> | |
} | |
} | |
// ./AnotherComponent.jsx (4th variation) | |
// Same as above, re-written | |
class AnotherComponent extends React.PureComponent { | |
shouldComponentUpdate(nextProps) { | |
return this.props !== nextProps | |
} | |
render() { | |
return <div>{this.props.someOtherProp}</div> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment