Move context to a "state" folder

This commit is contained in:
dannyrb 2020-05-17 21:41:57 -04:00
parent 96bf56b8c1
commit eecdb62dda
3 changed files with 8 additions and 47 deletions

View File

@ -1,47 +0,0 @@
import React, { useState, createContext, useContext } from 'react';
import PropTypes from 'prop-types';
const AppContext = createContext(null);
export const useAppContext = () => useContext(AppContext);
const AppContextProvider = ({ children }) => {
const [config, setConfig] = useState(null);
const get = ({ configKey = null }) => {
if (configKey) {
return config[configKey];
}
return config;
};
const add = ({ configKey, value }) => {
setConfig(s => ({
...s,
[configKey]: value,
}));
};
const remove = ({ configKey }) => {
setConfig(s => ({
...s,
[configKey]: null,
}));
};
return (
<AppContext.Provider value={{ get, add, remove }}>
{children}
</AppContext.Provider>
);
};
AppContextProvider.propTypes = {
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]).isRequired,
};
export default AppContextProvider;

View File

@ -0,0 +1,8 @@
import { createContext } from 'react';
export const APP_CONFIG_DEFAULT_VALUE = {
config: {},
setCurrentConfig: () => {},
};
export const appConfigContext = createContext(APP_CONFIG_DEFAULT_VALUE);