From 2657b06912642533c7df4bf76f1a5e64b2380f8a Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sat, 4 Sep 2021 05:06:35 +0300 Subject: [PATCH] Added immer as a dependency, replaced setState with more specific toggleFree function --- package.json | 1 + src/context.tsx | 12 +++++++++++- src/types/context.ts | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 688457d..4785400 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ }, "dependencies": { "@material-ui/core": "^4.12.3", + "immer": "^9.0.6", "react": "^17.0.2", "react-dom": "^17.0.2" } diff --git a/src/context.tsx b/src/context.tsx index d8a6813..4a966d0 100644 --- a/src/context.tsx +++ b/src/context.tsx @@ -1,3 +1,4 @@ +import produce from "immer"; import { createContext, FC, useContext, useState } from "react"; import { defaultState } from "./constants"; import { ContextData, ContextValue } from "./types/context"; @@ -7,8 +8,17 @@ const Context = createContext(undefined); export const RoomContextProvider: FC = ({ children }) => { const [state, setState] = useState(defaultState); + const toggleFree = (index: number) => + setState( + produce((draft) => { + draft.char[index].free = !draft.char[index].free; + }) + ); + return ( - {children} + + {children} + ); }; diff --git a/src/types/context.ts b/src/types/context.ts index 126e87d..485bb1f 100644 --- a/src/types/context.ts +++ b/src/types/context.ts @@ -8,5 +8,5 @@ export interface ContextData { export type ContextValue = { state: ContextData; - setState: Dispatch>; + toggleFree: (index: number) => void; };