Created
January 26, 2021 15:28
-
-
Save clintonmedbery/2a2b63e6bff48a27fdf3792cf7b68961 to your computer and use it in GitHub Desktop.
High Order Context
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
const ThingsWrapper = (props) => { | |
const { userID } = props | |
const { | |
things, | |
setThings | |
} = useThingsContext() | |
const [newThings] = useApi('GET', `${BASE_URL}/things`, null) | |
useEffect(() => { | |
if (newThings) { | |
setThings(newThings) | |
} | |
}, [newThings]) | |
return ( | |
<> | |
</> | |
) | |
} | |
export default ThingsWrapper |
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
export default withThings({ | |
props: { | |
foo: 'thing' | |
} | |
})(ComponentUsingThings) |
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
import { createContext, useContext } from 'react' | |
export const INITIAL_FOO_STATE = { | |
bar: 0 | |
} | |
export const ThingsContext = createContext({ | |
...INITIAL_FOO_STATE, | |
things: null, | |
setThings: () => {} | |
}) | |
export const useThingsContext = () => { | |
const context = useContext(ThingsContext) | |
return context | |
} |
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
const withThings = (props) => (InnerComponent) => (innerProps) => { | |
const [things, setThings] = useState() | |
const thingsObject = { | |
things, | |
setThings | |
} | |
return ( | |
<> | |
<Things.Provider value={thingsObject}> | |
<InnerComponent {...innerProps} /> | |
</SearchValuesContext.Provider> | |
</> | |
) | |
} | |
export default withThings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment