Skip to content

Commit

Permalink
feat: draft implementation of useStore in yahoo#733 (not production r…
Browse files Browse the repository at this point in the history
…eady)

Co-authored-by: pablopalacios <pablo.palacios@holidaycheck.com>
  • Loading branch information
raozixuan committed Oct 7, 2021
1 parent 92b0a32 commit 500c23c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/fluxible-addons-react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { default as createElementWithContext } from './createElementWithContext'
export { default as provideContext } from './provideContext';
export { default as useFluxible } from './useFluxible';
export { default as withFluxible } from './withFluxible';
export { default as useStore } from './useStore';
36 changes: 36 additions & 0 deletions packages/fluxible-addons-react/src/useStore.js
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;
30 changes: 30 additions & 0 deletions packages/fluxible-addons-react/tests/unit/lib/useStore.js
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');
});
});
});

0 comments on commit 500c23c

Please sign in to comment.