Skip to content
This repository has been archived by the owner on Nov 10, 2021. It is now read-only.

Commit

Permalink
Use useState instead of useReducer
Browse files Browse the repository at this point in the history
Also pull out increment into a constant to avoid the extra allocations
  • Loading branch information
ianobermiller committed Oct 15, 2019
1 parent c2ac6b4 commit 86279c2
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import {
createContext,
useContext,
useEffect,
useLayoutEffect,
useMemo,
useReducer,
useRef,
useLayoutEffect,
useEffect,
useState,
} from 'react';
import {Action, Dispatch, Store} from 'redux';
import shallowEqual from './shallowEqual';
Expand Down Expand Up @@ -88,7 +88,7 @@ export function create<

// Since we don't keep the derived state we still need to trigger
// an update when derived state changes.
const [, forceUpdate] = useReducer(x => x + 1, 0);
const [, forceUpdate] = useState(0);

// Keep previously commited derived state in a ref. Compare it to the new
// one when an action is dispatched and call forceUpdate if they are different.
Expand Down Expand Up @@ -126,8 +126,7 @@ export function create<
const newDerivedState = memoizedMapStateRef.current(store.getState());

if (!shallowEqual(newDerivedState, lastStateRef.current)) {
// In TS definitions userReducer's dispatch requires an argument
(forceUpdate as () => void)();
forceUpdate(increment);
}
};

Expand Down Expand Up @@ -163,3 +162,7 @@ export function create<
useMappedState,
};
}

function increment(x: number): number {
return x + 1;
}

0 comments on commit 86279c2

Please sign in to comment.