Skip to content

bugfix: fix eager state miss https://github.com/facebook/react/issues… #34065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HHsomeHand
Copy link

…/34061

Summary

I open a issue to explain this: #34061

==== the issue content ======

import React, { useState } from 'react';

export function App(props) {
  console.log('App');
  return <Child0 />;
}

function Child0(props) {
  let [state, setState] = useState(0);
  console.log('child0', state);
  return (
    <div onClick={() => setState(1)}>
      child0
      <Child1 />
    </div>
  );
}

function Child1(props) {
  console.log('child1');
  console.log('=======');
  return <div>child1</div>;
}

console:

App 
child0 0
child1 
=======  (first click)
child0 1
child1 
======= (second click) 
child0 1

(rest click: noop, eager state)

Strange, why is child0 1 printed one more time here?

Isn't there eager state? If the state hasn't changed, it shouldn't re-render, right?

I didn't use StrictMode

I debug react:

 function dispatchSetStateInternal(fiber, queue, action, lane) {
      var update = {
        lane: lane,
        revertLane: 0,
        action: action,
        hasEagerState: !1,
        eagerState: null,
        next: null
      };
      if (isRenderPhaseUpdate(fiber)) enqueueRenderPhaseUpdate(queue, update);
      else {
        var alternate = fiber.alternate;
        if (
          0 === fiber.lanes &&
          (null === alternate || 0 === alternate.lanes) &&
          ((alternate = queue.lastRenderedReducer), null !== alternate)
        ) {
          var prevDispatcher = ReactSharedInternals.H;
          ReactSharedInternals.H = InvalidNestedHooksDispatcherOnUpdateInDEV;
          try {
            var currentState = queue.lastRenderedState,
              eagerState = alternate(currentState, action);
            update.hasEagerState = !0;
            update.eagerState = eagerState;
            if (objectIs(eagerState, currentState))
              return (
                enqueueUpdate$1(fiber, queue, update, 0),
                null === workInProgressRoot &&
                  finishQueueingConcurrentUpdates(),
                !1
              );
          } catch (error) {
          } finally {
            ReactSharedInternals.H = prevDispatcher;
          }
        }
        action = enqueueConcurrentHookUpdate(fiber, queue, update, lane);
        if (null !== action)
          return (
            scheduleUpdateOnFiber(action, fiber, lane),
            entangleTransitionUpdate(action, queue, lane),
            !0
          );
      }
      return !1;
    }

I'm not using concurrent rendering either—so why does the 0 === fiber.lanes check fail?

Isn't fiber.lanes supposed to be cleared after the update is finished?

======

How did you test this change?

very small change, I use yarn test to run tests on my code.

Then I build it with the following command:

node ./scripts/rollup/build.js react/index,react/jsx,react.react-server,react-dom/index,react-dom/client,react-dom/server,react-dom.react-server,react-dom-server.node,react-dom-server-legacy.node,scheduler,react-server-dom-webpack/ --type=NODE_DEV,ESM_PROD,NODE_ES2015

After building, I link the react and react-dom packages to a project created with create-react-app. I initially tried linking them to a Vite project, but Vite threw some errors that vite can't use commonJS package.

However, the create-react-app project also throws an error:

Uncaught TypeError: Cannot read properties of null (reading 'useState')
    at exports.useState (react.development.js:1222:1)
    at Child0 (App.js:10:1)
    at Object.react_stack_bottom_frame (react-dom-client.development.js:28836:1)
    at renderWithHooks (react-dom-client.development.js:7742:1)
    at updateFunctionComponent (react-dom-client.development.js:10173:1)
    at beginWork (react-dom-client.development.js:11838:1)
    at runWithFiberInDEV (react-dom-client.development.js:990:1)
    at performUnitOfWork (react-dom-client.development.js:19212:1)
    at workLoopSync (react-dom-client.development.js:19042:1)
    at renderRootSync (react-dom-client.development.js:19023:1)

I didn’t modify useState, so this is really strange. Maybe I built it incorrectly?

but I run yarn test, only a few test didn't pass(17), like original project that i didn't modify anything.

Copy link

meta-cla bot commented Jul 31, 2025

Hi @HHsomeHand!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla meta-cla bot added the CLA Signed label Jul 31, 2025
Copy link

meta-cla bot commented Jul 31, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant