Skip to content

Commit 7e92a59

Browse files
Fix selector returning a function (#107)
1 parent 8d4021c commit 7e92a59

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/components/__tests__/hook.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ describe('Hook', () => {
210210
expect(children).toHaveBeenCalledTimes(1);
211211
expect(children).toHaveBeenCalledWith(undefined, actions);
212212
});
213+
214+
it('should support selectors returning a function on init and update', () => {
215+
const selector = state => ({ id }) => state[id];
216+
const { getMount, children } = setup({}, selector);
217+
getMount();
218+
219+
const newState = { count: 1 };
220+
storeStateMock.getState.mockReturnValue(newState);
221+
const update = storeStateMock.subscribe.mock.calls[0][0];
222+
act(() => update(storeStateMock.getState(), storeStateMock));
223+
224+
expect(children).toHaveBeenCalledWith(expect.any(Function), actions);
225+
});
213226
});
214227

215228
describe('createMemoizedSelector', () => {

src/components/hook.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function createHook(Store, { selector } = {}) {
5353
const currentState = stateSelector(storeState.getState(), propsArg);
5454
useDebugValue(currentState);
5555

56-
const triggerUpdate = useState(currentState)[1];
56+
const triggerUpdate = useState(() => currentState)[1];
5757
const propsRef = useRef(propsArg);
5858
propsRef.current = propsArg;
5959

@@ -70,7 +70,7 @@ export function createHook(Store, { selector } = {}) {
7070
const nextState = stateSelector(updatedState, propsRef.current);
7171

7272
if (nextState !== prevState) {
73-
triggerUpdate(nextState);
73+
triggerUpdate(() => nextState);
7474
prevState = nextState;
7575
}
7676
};

0 commit comments

Comments
 (0)