Use recoil for state management

This commit is contained in:
J Francisco Rader committed 2020-08-22 15:39:46 -03:00
1 parent 7a46eaf2be
commit d8f756b309
10 files changed
+63 -49

No files matched your search

+13 -11
View File
@@ -1,20 +1,22 @@
import { createContext, useContext, useEffect, useState } from "react";
import _set from "lodash/set";
import Default from "../default.json";
import { atom, useRecoilState } from "recoil";
export const ConfigContext = createContext(Default);
const configState = atom({
key: 'config',
default: Default
})
export const useConfig = (Context = ConfigContext) => {
const configCtx = useContext(Context);
const [config, _setConfig] = useState({ ...configCtx });
console.log(Default)
useEffect(() => {
_setConfig({ ...configCtx });
}, [configCtx]);
export const useConfig = (state = configState) => {
const [config, _setConfig] = useRecoilState(state)
const setConfig = (key, value) => {
const state = { ...config };
_setConfig(_set(state, key, value));
_setConfig(oldConfig => ({
...oldConfig,
[key]: value
}));
};
return [config, setConfig];
};