Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,44 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
expect(container.textContent).toEqual('NaN');
});

test('selector is not called when snaphot has not changed', async () => {
const store = createExternalStore({a: 0});

function selector(state) {
Scheduler.log('Selector');
return state.a;
}

function isEqual(a, b) {
return a === b;
}

function App() {
Scheduler.log('App');
const a = useSyncExternalStoreWithSelector(
store.subscribe,
store.getState,
null,
selector,
isEqual,
);
return <Text text={'A' + a} />;
}

const container = document.createElement('div');
const root = createRoot(container);
await act(() => root.render(<App />));
assertLog(['App', 'Selector', 'A0']);

await act(() => store.set(store.getState()));
await act(() => store.set({a: 0}));
assertLog(['Selector']);

await act(() => store.set(store.getState()));
await act(() => store.set(store.getState()));
assertLog([]);
});

describe('extra features implemented in user-space', () => {
// The selector implementation uses the lazy ref initialization pattern
// @gate !(enableUseRefAccessWarning && __DEV__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function useSyncExternalStoreWithSelector<Snapshot, Selection>(
// to React that the selections are conceptually equal, and we can bail
// out of rendering.
if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) {
memoizedSnapshot = nextSnapshot;
return prevSelection;
}

Expand Down