Skip to content

Instantly share code, notes, and snippets.

@frivolta
Created September 4, 2020 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frivolta/9a6a73f0857a90b6c53527602718ede2 to your computer and use it in GitHub Desktop.
Save frivolta/9a6a73f0857a90b6c53527602718ede2 to your computer and use it in GitHub Desktop.
useSidebarContext.tsx - typing-react-context-v2
import * as React from "react";
import { useSidebar, UseSidebar } from "./useSidebar";
import { createGenericContext } from "./createGenericContext";
interface Props {
children: React.ReactNode;
}
// Generate context
const [useSidebarContext, SidebarContextProvider] = createGenericContext<
UseSidebar
>();
// Generate provider
const SidebarProvider = ({ children }: Props) => {
const [isOpen, setIsOpen] = useSidebar(true);
return (
<SidebarContextProvider value={[isOpen, setIsOpen]}>
{children}
</SidebarContextProvider>
);
};
export { useSidebarContext, SidebarProvider };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment