UNPKG

lightweight-globalstate

Version:

An lightweight and easy state management hook for using React + Typescript

16 lines (10 loc) 592 B
import React, { createContext, useReducer, useContext } from 'react'; const GlobalStateContext = createContext({}); const stateReducer = () => (state, newState) => (Object.assign(Object.assign({}, state), newState)); const StateProvider = (props) => { const state = props.initialState ? props.initialState : {}; return (React.createElement(GlobalStateContext.Provider, { value: useReducer(stateReducer(), state) }, props.children)); }; const useGlobalState = () => useContext(GlobalStateContext); export { StateProvider, useGlobalState }; //# sourceMappingURL=index.es.js.map