Skip to content

Instantly share code, notes, and snippets.

@3nvi
Last active February 28, 2019 08:28
Show Gist options
  • Save 3nvi/537af1c6eed1031af19b289d301fafa3 to your computer and use it in GitHub Desktop.
Save 3nvi/537af1c6eed1031af19b289d301fafa3 to your computer and use it in GitHub Desktop.
Utilization of bail-out techniques
// 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