Skip to content

Instantly share code, notes, and snippets.

@frivolta
Created September 4, 2020 07:48
Show Gist options
  • Save frivolta/9e082ee8c30c8037170a0aa4b6d27284 to your computer and use it in GitHub Desktop.
Save frivolta/9e082ee8c30c8037170a0aa4b6d27284 to your computer and use it in GitHub Desktop.
useSidebarContext.tsx - typing-react-context-v1
import * as React from "react";
import { useSidebar, UseSidebar } from "./useSidebar";
interface Props {
children: React.ReactNode;
}
// Generate context
const SidebarContext = React.createContext<UseSidebar>(undefined!);
// Generate provider
const SidebarProvider = ({ children }: Props) => {
const [isOpen, setIsOpen] = useSidebar(true);
return (
<SidebarContext.Provider value={[isOpen, setIsOpen]}>
{children}
</SidebarContext.Provider>
);
};
// Custom context hook
const useSidebarContext = () => {
return React.useContext(SidebarContext);
};
export { SidebarProvider, useSidebarContext };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment