Skip to content

Commit

Permalink
refactor: Copy to useReducer & store Object accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Feb 13, 2025
1 parent c43002a commit 4f47033
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { options as _options } from 'preact';

const ObjectIs = Object.is;

/** @type {number} */
let currentIndex;

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

if (currentValue !== nextValue) {
if (!ObjectIs(currentValue, nextValue)) {
hookState._nextValue = [nextValue, hookState._value[1]];
hookState._component.setState({});
}
Expand Down Expand Up @@ -255,7 +257,8 @@ export function useReducer(reducer, initialState, init) {
const currentValue = hookItem._value[0];
hookItem._value = hookItem._nextValue;
hookItem._nextValue = undefined;
if (currentValue !== hookItem._value[0]) shouldUpdate = true;
if (!ObjectIs(currentValue, hookItem._value[0]))
shouldUpdate = true;
}
});

Expand Down Expand Up @@ -537,7 +540,7 @@ function argsChanged(oldArgs, newArgs) {
return (
!oldArgs ||
oldArgs.length !== newArgs.length ||
newArgs.some((arg, index) => !Object.is(arg, oldArgs[index]))
newArgs.some((arg, index) => !ObjectIs(arg, oldArgs[index]))
);
}

Expand Down

0 comments on commit 4f47033

Please sign in to comment.