Skip to content
Merged
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
20 changes: 6 additions & 14 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,23 +240,15 @@ export function useReducer(reducer, initialState, init) {
function updateHookState(p, s, c) {
if (!hookState._component.__hooks) return true;

/** @type {(x: import('./internal').HookState) => x is import('./internal').ReducerHookState} */
const isStateHook = x => !!x._component;
const stateHooks =
hookState._component.__hooks._list.filter(isStateHook);

const allHooksEmpty = stateHooks.every(x => !x._nextValue);
// When we have no updated hooks in the component we invoke the previous SCU or
// traverse the VDOM tree further.
if (allHooksEmpty) {
return prevScu ? prevScu.call(this, p, s, c) : true;
}
const hooksList = hookState._component.__hooks._list;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than doing excessive checks we just merge these paths


// We check whether we have components with a nextValue set that
// have values that aren't equal to one another this pushes
// us to update further down the tree
let shouldUpdate = hookState._component.props !== p;
stateHooks.forEach(hookItem => {
let shouldUpdate =
hookState._component.props !== p ||
hooksList.every(x => !x._nextValue);
hooksList.forEach(hookItem => {
if (hookItem._nextValue) {
const currentValue = hookItem._value[0];
hookItem._value = hookItem._nextValue;
Expand All @@ -275,7 +267,7 @@ export function useReducer(reducer, initialState, init) {
}
}

return hookState._nextValue || hookState._value;
return hookState._value;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're already converting _nextValue to _value in the _render hook

}

/**
Expand Down
Loading