Skip to content

Commit c5db0f6

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 e2dd222 commit c5db0f6

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

@@ -193,7 +195,7 @@ export function useReducer(reducer, initialState, init) {
193195
: hookState._value[0];
194196
const nextValue = hookState._reducer(currentValue, action);
195197

196-
if (currentValue !== nextValue) {
198+
if (!ObjectIs(currentValue, nextValue)) {
197199
hookState._nextValue = [nextValue, hookState._value[1]];
198200
hookState._component.setState({});
199201
}
@@ -258,7 +260,8 @@ export function useReducer(reducer, initialState, init) {
258260
const currentValue = hookItem._value[0];
259261
hookItem._value = hookItem._nextValue;
260262
hookItem._nextValue = undefined;
261-
if (currentValue !== hookItem._value[0]) shouldUpdate = true;
263+
if (!ObjectIs(currentValue, hookItem._value[0]))
264+
shouldUpdate = true;
262265
}
263266
});
264267

@@ -540,7 +543,7 @@ function argsChanged(oldArgs, newArgs) {
540543
return (
541544
!oldArgs ||
542545
oldArgs.length !== newArgs.length ||
543-
newArgs.some((arg, index) => arg !== oldArgs[index])
546+
newArgs.some((arg, index) => !ObjectIs(arg, oldArgs[index]))
544547
);
545548
}
546549

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
@@ -373,6 +373,32 @@ describe('useState', () => {
373373
expect(scratch.innerHTML).to.equal('<p>hello world!!!</p>');
374374
});
375375

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

0 commit comments

Comments
 (0)