Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ options._render = vnode => {
if (previousComponent === currentComponent) {
hooks._pendingEffects = [];
currentComponent._renderCallbacks = [];
hooks._list.forEach(hookItem => {
hooks._list.some(hookItem => {
if (hookItem._nextValue) {
hookItem._value = hookItem._nextValue;
}
Expand All @@ -83,7 +83,7 @@ options.diffed = vnode => {
const c = vnode._component;
if (c && c.__hooks) {
if (c.__hooks._pendingEffects.length) afterPaint(afterPaintEffects.push(c));
c.__hooks._list.forEach(hookItem => {
c.__hooks._list.some(hookItem => {
if (hookItem._pendingArgs) {
hookItem._args = hookItem._pendingArgs;
}
Expand Down Expand Up @@ -121,7 +121,7 @@ options.unmount = vnode => {
const c = vnode._component;
if (c && c.__hooks) {
let hasErrored;
c.__hooks._list.forEach(s => {
c.__hooks._list.some(s => {
try {
invokeCleanup(s);
} catch (e) {
Expand All @@ -133,6 +133,23 @@ options.unmount = vnode => {
}
};

/**
*
* @returns {import('./internal').HookState}
*/
function createHookState() {
return {
_value: undefined,
_args: undefined,
_cleanup: undefined,
_pendingArgs: undefined,
_component: undefined,
_reducer: undefined,
_context: undefined,
_factory: undefined
};
}

/**
* Get a hook's state from the currentComponent
* @param {number} index The index of the hook to get
Expand All @@ -158,7 +175,7 @@ function getHookState(index, type) {
});

if (index >= hooks._list.length) {
hooks._list.push({});
hooks._list.push(createHookState());
}

return hooks._list[index];
Expand Down Expand Up @@ -248,7 +265,7 @@ export function useReducer(reducer, initialState, init) {
let shouldUpdate =
hookState._component.props !== p ||
hooksList.every(x => !x._nextValue);
hooksList.forEach(hookItem => {
hooksList.some(hookItem => {
if (hookItem._nextValue) {
const currentValue = hookItem._value[0];
hookItem._value = hookItem._nextValue;
Expand Down
2 changes: 0 additions & 2 deletions hooks/src/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export type HookState =
interface BaseHookState {
_value?: unknown;
_nextValue?: unknown;
_pendingValue?: unknown;
_args?: unknown;
_pendingArgs?: unknown;
_component?: unknown;
Expand All @@ -75,7 +74,6 @@ export interface EffectHookState extends BaseHookState {

export interface MemoHookState<T = unknown> extends BaseHookState {
_value?: T;
_pendingValue?: T;
_args?: unknown[];
_pendingArgs?: unknown[];
_factory?: () => T;
Expand Down
Loading