Skip to content

Instantly share code, notes, and snippets.

@jvorcak
Created August 27, 2018 14:12
Show Gist options
  • Save jvorcak/c6ed91a5f0211690dff01831d5b23c39 to your computer and use it in GitHub Desktop.
Save jvorcak/c6ed91a5f0211690dff01831d5b23c39 to your computer and use it in GitHub Desktop.
Key Setter
/*
This function can be used in `compose` to decide on a key for a given component
@example:
export default compose(
connect(state => ({
a: state.a,
b: state.b,
})),
KeySetter(props => `${props.a}-${props.b}`), // remounts a component if one of `a` or `b` changes.
)(A)
@see https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key
*/
export const KeySetter = keyFn => WrappedComponent => props =>
<WrappedComponent key={keyFn(props)} {...props} />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment