Skip to content

Commit d73e964

Browse files
rschristianJoviDeCroockjayrobin
committed
refactor: Switch to Object.is for hook args (#4663)
* Remove unused imports * refactor: Switch to Object.is for hook args * refactor: Copy to `useReducer` & store `Object` accessor * test: Add tests for `useEffect` & `useState` w/ `NaN` Co-authored-by: jayrobin <[email protected]> --------- Co-authored-by: jdecroock <[email protected]> Co-authored-by: jayrobin <[email protected]>
1 parent 3b44926 commit d73e964

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

hooks/src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { options as _options } from 'preact';
22

3+
const ObjectIs = Object.is;
4+
35
/** @type {number} */
46
let currentIndex;
57

@@ -190,7 +192,7 @@ export function useReducer(reducer, initialState, init) {
190192
: hookState._value[0];
191193
const nextValue = hookState._reducer(currentValue, action);
192194

193-
if (currentValue !== nextValue) {
195+
if (!ObjectIs(currentValue, nextValue)) {
194196
hookState._nextValue = [nextValue, hookState._value[1]];
195197
hookState._component.setState({});
196198
}
@@ -255,7 +257,8 @@ export function useReducer(reducer, initialState, init) {
255257
const currentValue = hookItem._value[0];
256258
hookItem._value = hookItem._nextValue;
257259
hookItem._nextValue = undefined;
258-
if (currentValue !== hookItem._value[0]) shouldUpdate = true;
260+
if (!ObjectIs(currentValue, hookItem._value[0]))
261+
shouldUpdate = true;
259262
}
260263
});
261264

@@ -537,7 +540,7 @@ function argsChanged(oldArgs, newArgs) {
537540
return (
538541
!oldArgs ||
539542
oldArgs.length !== newArgs.length ||
540-
newArgs.some((arg, index) => arg !== oldArgs[index])
543+
newArgs.some((arg, index) => !ObjectIs(arg, oldArgs[index]))
541544
);
542545
}
543546

hooks/test/browser/useEffect.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,4 +636,26 @@ describe('useEffect', () => {
636636
expect(calls.length).to.equal(1);
637637
expect(calls).to.deep.equal(['doing effecthi']);
638638
});
639+
640+
it('should not rerun when receiving NaN on subsequent renders', () => {
641+
const calls = [];
642+
const Component = ({ value }) => {
643+
const [count, setCount] = useState(0);
644+
useEffect(() => {
645+
calls.push('doing effect' + count);
646+
setCount(count + 1);
647+
return () => {
648+
calls.push('cleaning up' + count);
649+
};
650+
}, [value]);
651+
return <p>{count}</p>;
652+
};
653+
const App = () => <Component value={NaN} />;
654+
655+
act(() => {
656+
render(<App />, scratch);
657+
});
658+
expect(calls.length).to.equal(1);
659+
expect(calls).to.deep.equal(['doing effect0']);
660+
});
639661
});

hooks/test/browser/useState.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,32 @@ describe('useState', () => {
372372
expect(scratch.innerHTML).to.equal('<p>hello world!!!</p>');
373373
});
374374

375+
it('should limit rerenders when setting state to NaN', () => {
376+
const calls = [];
377+
const App = ({ i }) => {
378+
calls.push('rendering' + i);
379+
const [greeting, setGreeting] = useState(0);
380+
381+
if (i === 2) {
382+
setGreeting(NaN);
383+
}
384+
385+
return <p>{greeting}</p>;
386+
};
387+
388+
act(() => {
389+
render(<App i={1} />, scratch);
390+
});
391+
expect(calls.length).to.equal(1);
392+
expect(calls).to.deep.equal(['rendering1']);
393+
394+
act(() => {
395+
render(<App i={2} />, scratch);
396+
});
397+
expect(calls.length).to.equal(3);
398+
expect(calls.slice(1).every(c => c === 'rendering2')).to.equal(true);
399+
});
400+
375401
describe('Global sCU', () => {
376402
let prevScu;
377403
before(() => {

0 commit comments

Comments
 (0)