Created
August 27, 2018 14:12
-
-
Save jvorcak/c6ed91a5f0211690dff01831d5b23c39 to your computer and use it in GitHub Desktop.
Key Setter
This file contains hidden or 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
/* | |
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