Skip to content

Commit

Permalink
use sync external store requires stable getSnapshot results
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Aug 8, 2022
1 parent cb50040 commit b1d3ac3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion compat/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function useSyncExternalStore(subscribe, getSnapshot) {

useLayoutEffect(() => {
if (value !== state) {
setState(value);
setState(() => value);
}
}, [subscribe, value, getSnapshot]);

Expand Down
12 changes: 7 additions & 5 deletions compat/test/browser/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('React-18-hooks', () => {
});
expect(scratch.innerHTML).to.equal('<p>hello world</p>');
expect(subscribe).to.be.calledOnce;
expect(getSnapshot).to.be.calledOnce;
expect(getSnapshot).to.be.calledTwice;
});

it('subscribes and rerenders when called', () => {
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('React-18-hooks', () => {
});
expect(scratch.innerHTML).to.equal('<p>hello world</p>');
expect(subscribe).to.be.calledOnce;
expect(getSnapshot).to.be.calledOnce;
expect(getSnapshot).to.be.calledTwice;

called = true;
flush();
Expand All @@ -137,9 +137,11 @@ describe('React-18-hooks', () => {
return () => {};
});

const func = () => 'value: ' + i++;

let i = 0;
const getSnapshot = sinon.spy(() => {
return () => 'value: ' + i++;
return func;
});

const App = () => {
Expand All @@ -152,12 +154,12 @@ describe('React-18-hooks', () => {
});
expect(scratch.innerHTML).to.equal('<p>value: 0</p>');
expect(subscribe).to.be.calledOnce;
expect(getSnapshot).to.be.calledOnce;
expect(getSnapshot).to.be.calledTwice;

flush();
rerender();

expect(scratch.innerHTML).to.equal('<p>value: 1</p>');
expect(scratch.innerHTML).to.equal('<p>value: 0</p>');
});

it('works with useCallback', () => {
Expand Down

0 comments on commit b1d3ac3

Please sign in to comment.