-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make Provider and hooks directly available from package * 2.0.0-beta.0 * Remove Providers from stateDataList * Update documentation for version 2.0 * Make version 2.0 stable
- Loading branch information
1 parent
862fb83
commit 81cedbd
Showing
11 changed files
with
259 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as React from 'react'; | ||
import { createContext, useContext } from 'react'; | ||
import { IProvider, ProviderProps } from './types'; | ||
import createGlobalState from './create-global-state'; | ||
import CombineProviders from './utils/CombineProviders'; | ||
|
||
interface StateObject { | ||
[key: string]: any; | ||
} | ||
|
||
interface StateData<T> { | ||
stateHook: () => T; | ||
updaterHook: () => React.Dispatch<React.SetStateAction<T>>; | ||
} | ||
|
||
interface StateDataList { | ||
[key: string]: StateData<any>; | ||
} | ||
|
||
interface RhinoProviderProps extends ProviderProps { | ||
initialStates: StateObject; | ||
} | ||
|
||
const StateDataContext = createContext<StateDataList>({}); | ||
|
||
const RhinoProvider: React.FC<RhinoProviderProps> = ({ | ||
children, | ||
initialStates: initialStatesObject, | ||
}: RhinoProviderProps) => { | ||
const stateDataList: StateDataList = {}; | ||
const ProviderList: Array<IProvider> = []; | ||
|
||
Object.keys(initialStatesObject).forEach((key) => { | ||
const initialValue = initialStatesObject[key]; | ||
const { Provider, useStateValue, useStateUpdate } = createGlobalState(initialValue); | ||
|
||
stateDataList[key] = { | ||
stateHook: useStateValue, | ||
updaterHook: useStateUpdate, | ||
}; | ||
|
||
ProviderList.push(Provider); | ||
}); | ||
|
||
return ( | ||
<CombineProviders providers={ProviderList}> | ||
<StateDataContext.Provider value={stateDataList}>{children}</StateDataContext.Provider> | ||
</CombineProviders> | ||
); | ||
}; | ||
|
||
export const useStateDataList = () => useContext(StateDataContext); | ||
|
||
export default RhinoProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useStateDataList } from '../RhinoProvider'; | ||
import useSetRhinoState from './useSetRhinoState'; | ||
import useRhinoValue from './useRhinoValue'; | ||
|
||
/** | ||
* | ||
* React hook which returns the stateful value corresponding to the key | ||
* provided and a function to update it | ||
* @param key Key which represents the state | ||
*/ | ||
function useRhinoState<T>(key: string): [T, React.Dispatch<React.SetStateAction<T>>] { | ||
const stateDataList = useStateDataList(); | ||
|
||
if (key in stateDataList) { | ||
return [useRhinoValue(key), useSetRhinoState(key)]; | ||
} | ||
throw new Error(`${key} is an invalid key`); | ||
} | ||
|
||
export default useRhinoState; |
Oops, something went wrong.