File tree Expand file tree Collapse file tree 3 files changed +28
-5
lines changed Expand file tree Collapse file tree 3 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -13,10 +13,11 @@ This is a very simple implementation using a combination of [useEffect](https://
1313
1414const ** value_from_store** = (** prevState** , ** store** ) => {...}
1515
16- const ** value** = ** useFluxStore** (** store** : \< FluxStore>, ** value_from_store** : Function)
16+ const ** value** = ** useFluxStore** (** store** : \< FluxStore>, ** value_from_store** : Function, * deps * )
1717
18- ## Usage
18+ # Usage
1919
20+ ## Basic Usage
2021Using the [ CounterStore example] ( https://facebook.github.io/flux/docs/flux-utils.html#content ) from Flux Utils.
2122
2223~~~
@@ -28,3 +29,22 @@ const CounterComponent = () => {
2829 return <CounterUI counter={counter} />;
2930}
3031~~~
32+
33+ ## Dependencies
34+
35+ There are cases where the reducer is using other State/Prop values. Normally useReducer would not trigger a dispatch in this case. We use the ** deps** parameter of useFluxStore as a list.
36+
37+ ~~~
38+ import useFluxStore from 'flux-hooks';
39+ const SearchComponent = () => {
40+ const [query, setQuery] = useState("")
41+ const results = useFluxStore(SearchStore, (prevState, store) => store.getSearchResults(query))
42+
43+ return <div>
44+ <input type="text" value={query} onChange={e => setQuery(e.target.value)} />
45+ <ul>
46+ results.map(r => <li>{r}</li>)
47+ </ul>
48+ </div>
49+ }
50+ ~~~
Original file line number Diff line number Diff line change 1- import { useEffect , useReducer } from 'react' ;
1+ import { useEffect , useReducer , useMemo } from 'react' ;
22
33// useFluxStore essentially combines useReducer and useEffect to use with FluxStores
44// useReducer: Used to extract relevant values from the store
55// useEffect is used to attach a listener to the store
66
7- export function useFluxStore ( store , reducer ) {
7+ export function useFluxStore ( store , reducer , deps = [ ] ) {
88 // Call useReducer and set initial value from current state of store.
99
1010 // We need to pass reducer(null, store) as initialArg otherwise the first out will be undefined
1111 const [ out , _dispatch ] = useReducer ( reducer , reducer ( null , store ) ) ;
1212
13+ // For any dependencies in the reducer, we make sure to trigger the reducer again
14+ useMemo ( ( ) => { _dispatch ( store ) ; } , deps ) ;
15+
1316 useEffect ( ( ) => {
1417 function listener ( ) {
1518 // When store is updated, we dispatch an update to the reducer
Original file line number Diff line number Diff line change 11{
22 "name" : " flux-hooks" ,
3- "version" : " 1.0.3 " ,
3+ "version" : " 1.0.4 " ,
44 "description" : " Hooks implementation for Facebook Flux Util's Stores" ,
55 "main" : " index.js" ,
66 "scripts" : {
You can’t perform that action at this time.
0 commit comments