Skip to content

Instantly share code, notes, and snippets.

@dantman
Created March 22, 2019 02:21
Show Gist options
  • Save dantman/68ebdd1c1ebb9514064a5914dfd955be to your computer and use it in GitHub Desktop.
Save dantman/68ebdd1c1ebb9514064a5914dfd955be to your computer and use it in GitHub Desktop.
React context slice idea
MyContext.slice(value => value.foo);
const KeyContext = useMemo(() => MyContext.slice(value => value[key]), [key])
const keyValue = useContext(KeyContext);
const KeyContext = useMemo(() => MyContext.slice(value => value[key]), [key])
return <KeyContext.Consumer>{keyValue => keyValue}</KeyContext.Consumer>
@decorate(memoizeOne)
getKeyContext(key) {
return MyContext.slice(value => value[key]);
}
render() {
const {key} = this.props;
const KeyContext = this.getKeyContext();
return <KeyContext.Consumer>{keyValue => keyValue}</KeyContext.Consumer>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment