Skip to content

Instantly share code, notes, and snippets.

@clintonmedbery
Created January 26, 2021 15:28
Show Gist options
  • Save clintonmedbery/2a2b63e6bff48a27fdf3792cf7b68961 to your computer and use it in GitHub Desktop.
Save clintonmedbery/2a2b63e6bff48a27fdf3792cf7b68961 to your computer and use it in GitHub Desktop.
High Order Context
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
export default withThings({
props: {
foo: 'thing'
}
})(ComponentUsingThings)
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
}
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