Last active
April 20, 2020 09:24
-
-
Save davidmz/70cb9726e882f12b6b19710c4582aef7 to your computer and use it in GitHub Desktop.
React context that can be updated from the consumer
This file contains 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 React, { createContext, useState, useCallback } from 'react'; | |
export const context = createContext({ | |
query: '', | |
setQuery: () => undefined, | |
}); | |
export function Provider({ children }) { | |
const [query, setStateQuery] = useState(''); | |
const setQuery = useCallback((q) => setStateQuery(q), []); | |
return <context.Provider value={{ query, setQuery }}>{children}</context.Provider>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment