forked from yahoo/fluxible
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: draft implementation of useStore in yahoo#733 (not production r…
…eady) Co-authored-by: pablopalacios <[email protected]>
- Loading branch information
raozixuan
committed
Oct 7, 2021
1 parent
92b0a32
commit 500c23c
Showing
3 changed files
with
67 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {useEffect, useState} from 'react'; | ||
import useFluxible from './useFluxible' | ||
|
||
/** | ||
* React hook that returns a state from Fluxible store. | ||
* TODO: this is a draft for an ongoing discussion in #733 | ||
* | ||
* Example: | ||
* | ||
* const FooComponent = () => { | ||
* const getStateFromStore = store => store.getFoo(); | ||
* const foo = useStore('FooStore', getStateFromStore); | ||
* return <p id={foo} />; | ||
* }; | ||
* | ||
* @function useFluxible | ||
* @returns {object} - a state from Fluxible store | ||
*/ | ||
const useStore = (storeName, getStateFromStore) => { | ||
const { getStore } = useFluxible(); | ||
const store = getStore(storeName); | ||
const [state, setState] = useState(getStateFromStore(store)); | ||
|
||
function updateState() { | ||
setState(getStateFromStore(store)); | ||
} | ||
|
||
useEffect(() => { | ||
store.on('change', updateState); | ||
return () => store.removeListener('change', updateState); | ||
}, [store, updateState]); | ||
|
||
return state; | ||
} | ||
|
||
export default useStore; |
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,30 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import TestRenderer from 'react-test-renderer'; | ||
import createMockComponentContext from 'fluxible/utils/createMockComponentContext'; | ||
import FooStore from '../../fixtures/stores/FooStore'; | ||
import { useStore, FluxibleProvider } from '../../../'; | ||
|
||
describe('fluxible-addons-react', () => { | ||
describe('useStore', () => { | ||
it('returns fluxible store', () => { | ||
const FooComponent = () => { | ||
const getStateFromStore = store => store.getFoo(); | ||
const foo = useStore('FooStore', getStateFromStore); | ||
return <p id={foo} />; | ||
}; | ||
|
||
const context = createMockComponentContext({ stores: [FooStore] }); | ||
|
||
const testRenderer = TestRenderer.create( | ||
<FluxibleProvider context={context}> | ||
<FooComponent /> | ||
</FluxibleProvider> | ||
); | ||
|
||
const component = testRenderer.root.findByType('p'); | ||
|
||
expect(component.props.id).to.deep.equal('bar'); | ||
}); | ||
}); | ||
}); |