facebook / react

The library for web and native user interfaces.
https://react.dev
MIT License
229.25k stars 46.95k forks source link

Bug: renderwithhooks executes twice #25028

Closed qzruncode closed 2 years ago

qzruncode commented 2 years ago
/**
 * This is the source code of react version 8.2
 * I omitted part of the code
 * Through debugging, I found that renderwithhooks was executed twice, and there was no difference between the two executions. Why did you do this? I understand that this is a bug
 */
function mountIndeterminateComponent(_current, workInProgress, Component, renderLanes) {
  // ...
  value = renderWithHooks(null, workInProgress, Component, props, context, renderLanes); /* first */
  hasId = checkDidRenderIdHook();

  workInProgress.flags |= PerformedWork;

  if ( typeof value === 'object' && value !== null && typeof value.render === 'function' && value.$$typeof === undefined) { 
    // ...
  } else {
    workInProgress.tag = FunctionComponent;
    {
      if ( workInProgress.mode & StrictLegacyMode) {
        try {
          value = renderWithHooks(null, workInProgress, Component, props, context, renderLanes); /* second */
          hasId = checkDidRenderIdHook();
        } finally {
          setIsStrictModeForDevtools(false);
        }
      }
    }

    return workInProgress.child;
  }
}
eps1lon commented 2 years ago

This is intended behavior caused by wrapping your app in StrictMode. Specifically to detect side-effects during render.