Skip to content

Instantly share code, notes, and snippets.

@Dodsaren
Last active August 21, 2020 10:26
Show Gist options
  • Save Dodsaren/aa11b12e0bf5c35c7346c41adec670d5 to your computer and use it in GitHub Desktop.
Save Dodsaren/aa11b12e0bf5c35c7346c41adec670d5 to your computer and use it in GitHub Desktop.
Example of "global" hook utilizing react context
import React, { useContext, useState } from 'react';
const initialState = 'Jimbo';
const JimboContext = React.createContext(initialState, () => {});
export const JimboProvider = ({ children }) => {
const [state, setState] = useState(initialState);
return (
<JimboContext.Provider value={[state, setState]}>
{children}
</JimboContext.Provider>
);
};
const useJimbo = () => {
const [state, setState] = useContext(JimboContext);
return [state, setState];
};
export default useJimbo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment