Skip to content

Instantly share code, notes, and snippets.

@NoamELB
Last active September 25, 2018 11:00
Show Gist options
  • Save NoamELB/1588ea8398d989a0841ad06773e379e1 to your computer and use it in GitHub Desktop.
Save NoamELB/1588ea8398d989a0841ad06773e379e1 to your computer and use it in GitHub Desktop.
component that should not update and will not update when not intended
class ShouldNotUpdate extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.type !== nextProps.type;
}
render() {
return 'I am rendering...';
}
}
function Parent({someCondition}) {
let child, type;
if (someCondition) {
child = <div className="type_b" />;
type = 'b';
} else {
child = <div className="type_a" />;
type = 'a';
}
return (
<ShouldNotUpdate type={type}>
{child}
</ShouldNotUpdate>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment