diff --git a/src/SidebarProvider.tsx b/src/SidebarProvider.tsx index cc44827..239602b 100644 --- a/src/SidebarProvider.tsx +++ b/src/SidebarProvider.tsx @@ -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 ({ + current, + setCurrent, + }), [current, setCurrent]) return ( - + {children} ) diff --git a/src/components/ResourcesSidebar/index.tsx b/src/components/ResourcesSidebar/index.tsx index 9372009..cdf900d 100644 --- a/src/components/ResourcesSidebar/index.tsx +++ b/src/components/ResourcesSidebar/index.tsx @@ -1,5 +1,5 @@ import { graphql, Link, useStaticQuery } from "gatsby" -import React, { PropsWithChildren, useState } from "react" +import React, { PropsWithChildren, useState, memo } from "react" import TriangleDown from "../../icons/triangle-down.svg" import Banner from "../../images/tph-banner.svg" import useSidebar from "./../../hooks/useSidebar" @@ -91,7 +91,7 @@ function Folder({ item }: { item: IFolder }) { ) } -function FirstLevelFolder({ item, index }: { item: IFolder; index: number }) { +const FirstLevelFolder = memo(({ item, index }: { item: IFolder; index: number }) => { const { current, setCurrent } = useSidebar() const collapsed = current !== index @@ -107,7 +107,7 @@ function FirstLevelFolder({ item, index }: { item: IFolder; index: number }) { {item.children.map(node => plantTree(node))} ) -} +}) function MenuItem({ children, to }: PropsWithChildren<{ to: string }>) { return ( diff --git a/src/hooks/useSidebar.tsx b/src/hooks/useSidebar.tsx index ab3a4b4..4344d4a 100644 --- a/src/hooks/useSidebar.tsx +++ b/src/hooks/useSidebar.tsx @@ -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) {