Skip to content

Instantly share code, notes, and snippets.

@davidmz
Last active April 20, 2020 09:24
Show Gist options
  • Save davidmz/70cb9726e882f12b6b19710c4582aef7 to your computer and use it in GitHub Desktop.
Save davidmz/70cb9726e882f12b6b19710c4582aef7 to your computer and use it in GitHub Desktop.
React context that can be updated from the consumer
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