Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
update SpaceKitProvider to react to updated props
Browse files Browse the repository at this point in the history
  • Loading branch information
daniman committed Jun 2, 2021
1 parent cad32f6 commit 5c4b2b0
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/SpaceKitProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import React, { useEffect, useMemo } from "react";
import isEqual from "lodash/isEqual";

interface State {
/**
Expand Down Expand Up @@ -33,7 +34,7 @@ const defaultState: State = {

const SpaceKitStateContext = React.createContext<State | undefined>(undefined);
const SpaceKitSetContext = React.createContext<
React.Dispatch<React.SetStateAction<State | undefined>> | undefined
React.Dispatch<React.SetStateAction<State>> | undefined
>(undefined);

/**
Expand All @@ -47,12 +48,25 @@ const SpaceKitSetContext = React.createContext<
*/
export const SpaceKitProvider: React.FC<Partial<State>> = ({
children,
...stateProps
theme,
disableAnimations,
}) => {
const [state, setState] = React.useState<State | undefined>({
...defaultState,
...stateProps,
});
const nextState = useMemo(
() => ({
theme: theme ?? defaultState.theme,
disableAnimations: disableAnimations ?? defaultState.disableAnimations,
singletonComponents: {},
}),
[theme, disableAnimations],
);
const [state, setState] = React.useState<State>(nextState);

useEffect(() => {
setState((currState) => ({
...nextState,
singletonComponents: currState?.singletonComponents,
}));
}, [nextState]);

return (
<SpaceKitStateContext.Provider value={state}>
Expand Down

0 comments on commit 5c4b2b0

Please sign in to comment.