-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
132 additions
and
126 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
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
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,5 @@ | ||
import { createContext } from 'react' | ||
|
||
const EasyPeasyContext = createContext() | ||
|
||
export default EasyPeasyContext |
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,15 @@ | ||
import { useState, useEffect, useMemo, useContext } from 'react' | ||
import EasyPeasyContext from './context' | ||
|
||
export function useStore(mapState) { | ||
const store = useContext(EasyPeasyContext) | ||
const [state, setState] = useState(mapState(store.getState())) | ||
const result = useMemo(() => state, [state]) | ||
useEffect(() => store.subscribe(() => setState(mapState(store.getState())))) | ||
return result | ||
} | ||
|
||
export function useAction(mapActions) { | ||
const store = useContext(EasyPeasyContext) | ||
return mapActions(store.dispatch) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { createStore, effect, select } from './easy-peasy' | ||
import { useStore, useAction } from './hooks' | ||
import EasyPeasyProvider from './provider' | ||
|
||
export { createStore, effect, select } | ||
export { createStore, effect, select, useStore, useAction, EasyPeasyProvider } |
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,16 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import EasyPeasyContext from './context' | ||
|
||
const EasyPeasyProvider = ({ children, store }) => ( | ||
<EasyPeasyContext.Provider value={store}> | ||
{children} | ||
</EasyPeasyContext.Provider> | ||
) | ||
|
||
EasyPeasyProvider.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
store: PropTypes.object.isRequired, | ||
} | ||
|
||
export default EasyPeasyProvider |
Oops, something went wrong.