Fixing up the sidebar context

This commit is contained in:
Phi
2019-08-23 14:04:38 -04:00
parent 5ad58fe5dc
commit bf0df576d2
2 changed files with 9 additions and 13 deletions
+7 -11
View File
@@ -1,4 +1,4 @@
import React, { useState } from "react"
import React, { useState, useMemo } from "react"
interface ISidebarProviderProps {
children: React.ReactNode
@@ -15,20 +15,16 @@ export const SidebarContext = React.createContext<ISidebarContextInterface | nul
export function SidebarProvider({
children,
}: ISidebarProviderProps): JSX.Element {
}: ISidebarProviderProps) {
const [current, setCurrent] = useState(0)
function selectFirstLevel(index: number) {
setCurrent(index)
}
const memoizedContextValue = useMemo(() => ({
current,
setCurrent,
}), [current, setCurrent])
return (
<SidebarContext.Provider
value={{
current,
setCurrent: selectFirstLevel,
}}
>
<SidebarContext.Provider value={memoizedContextValue}>
{children}
</SidebarContext.Provider>
)
+2 -2
View File
@@ -1,7 +1,7 @@
import { useContext } from "react"
import { SidebarContext, SidebarContextInterface } from "../SidebarProvider"
import { SidebarContext, ISidebarContextInterface } from "../SidebarProvider"
export default function useSidebar(): SidebarContextInterface {
export default function useSidebar(): ISidebarContextInterface {
const sidebarContext = useContext(SidebarContext)
if (!sidebarContext) {