Skip to content

Commit a2d6574

Browse files
committed
Update src/constants.js
1 parent 667a39b commit a2d6574

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

hooks/src/index.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { options as _options } from 'preact';
2-
import { SKIP_CHILDREN } from 'preact/src/constants';
2+
import { SKIP_CHILDREN } from '../../src/constants';
33

44
/** @type {number} */
55
let currentIndex;
@@ -25,6 +25,7 @@ let oldAfterDiff = options.diffed;
2525
let oldCommit = options._commit;
2626
let oldBeforeUnmount = options.unmount;
2727
let oldRoot = options._root;
28+
let oldAfterRender = options._afterRender;
2829

2930
const RAF_TIMEOUT = 100;
3031
let prevRaf;
@@ -194,12 +195,10 @@ export function useReducer(reducer, initialState, init) {
194195

195196
if (hookState._actions.length) {
196197
const initialValue = hookState._value[0];
197-
hookState._actions.forEach(action => {
198-
const value = hookState._value[0];
199-
hookState._value[0] = hookState._reducer(value, action);
198+
hookState._actions.some(action => {
199+
hookState._value[0] = hookState._reducer(hookState._value[0], action);
200200
});
201201

202-
hookState._actions = [];
203202
// This is incorrect, we set this shallowly based on the last state
204203
// which means that if 2 useReducer calls have actions and the last one
205204
// settles to an equal state we bail the previous one as well. This should
@@ -208,19 +207,19 @@ export function useReducer(reducer, initialState, init) {
208207
hookState._didUpdate = initialValue !== hookState._value[0];
209208
hookState._value = [hookState._value[0], hookState._value[1]];
210209
hookState._didExecute = true;
210+
hookState._actions = [];
211211
}
212212

213213
return hookState._value;
214214
}
215215

216-
const oldAfterRender = options._afterRender;
217216
options._afterRender = (newVNode, oldVNode) => {
218217
if (newVNode._component && newVNode._component.__hooks) {
219218
const hooks = newVNode._component.__hooks._list;
220219
const stateHooksThatExecuted = hooks.filter(
221220
/** @type {(x: import('./internal').HookState) => x is import('./internal').ReducerHookState} */
222221
// @ts-expect-error
223-
x => !!x._component && x._didExecute
222+
x => x._component && x._didExecute
224223
);
225224

226225
if (
@@ -232,9 +231,8 @@ options._afterRender = (newVNode, oldVNode) => {
232231
newVNode._flags |= SKIP_CHILDREN;
233232
}
234233

235-
stateHooksThatExecuted.forEach(hook => {
236-
hook._didUpdate = undefined;
237-
hook._didExecute = false;
234+
stateHooksThatExecuted.some(hook => {
235+
hook._didExecute = hook._didUpdate = false;
238236
});
239237
}
240238

src/constants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const INSERT_VNODE = 1 << 2;
77
/** Indicates a VNode has been matched with another VNode in the diff */
88
export const MATCHED = 1 << 1;
99

10-
export const SKIP_CHILDREN = 1 << 18;
10+
export const SKIP_CHILDREN = 1 << 3;
1111

1212
/** Reset all mode flags */
1313
export const RESET_MODE = ~(MODE_HYDRATE | MODE_SUSPENDED);

0 commit comments

Comments
 (0)