mirror of
https://github.com/Stone-Red-Code/website.git
synced 2026-09-07 23:43:57 +02:00
Fixing up the sidebar context
This commit is contained in:
+7
-11
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState } from "react"
|
import React, { useState, useMemo } from "react"
|
||||||
|
|
||||||
interface ISidebarProviderProps {
|
interface ISidebarProviderProps {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
@@ -15,20 +15,16 @@ export const SidebarContext = React.createContext<ISidebarContextInterface | nul
|
|||||||
|
|
||||||
export function SidebarProvider({
|
export function SidebarProvider({
|
||||||
children,
|
children,
|
||||||
}: ISidebarProviderProps): JSX.Element {
|
}: ISidebarProviderProps) {
|
||||||
const [current, setCurrent] = useState(0)
|
const [current, setCurrent] = useState(0)
|
||||||
|
|
||||||
function selectFirstLevel(index: number) {
|
const memoizedContextValue = useMemo(() => ({
|
||||||
setCurrent(index)
|
current,
|
||||||
}
|
setCurrent,
|
||||||
|
}), [current, setCurrent])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SidebarContext.Provider
|
<SidebarContext.Provider value={memoizedContextValue}>
|
||||||
value={{
|
|
||||||
current,
|
|
||||||
setCurrent: selectFirstLevel,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{children}
|
{children}
|
||||||
</SidebarContext.Provider>
|
</SidebarContext.Provider>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useContext } from "react"
|
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)
|
const sidebarContext = useContext(SidebarContext)
|
||||||
|
|
||||||
if (!sidebarContext) {
|
if (!sidebarContext) {
|
||||||
|
|||||||
Reference in New Issue
Block a user