Skip to content
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

Bug: Value printed to console is rendered incorrectly in StrictMode #22095

Closed
Yakimych opened this issue Aug 15, 2021 · 2 comments
Closed

Bug: Value printed to console is rendered incorrectly in StrictMode #22095

Yakimych opened this issue Aug 15, 2021 · 2 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@Yakimych
Copy link

Yakimych commented Aug 15, 2021

React version: 17.0.2

Steps To Reproduce

The following minimal repro sample is used to indicate a props change in a component:

import React from "react";

export default function App() {
  const [a, setA] = React.useState("a");

  return (
    <>
      <Test a={a} />
      <button onClick={() => setA((oldA) => (oldA === "a" ? "b" : "a"))}>
        Test
      </button>
    </>
  );
}

const Test = ({ a }) => {
  const oldRef = React.useRef(a);

  const hasChanged = oldRef.current !== a;
  if (hasChanged) {
    oldRef.current = a;
  }

  const changedText = hasChanged ? "yes" : "no";
  console.log("Changed: ", changedText);

  return (
    <>
      <div>{a}</div>
      <div>Changed: {changedText}</div>
    </>
  );
};

Clicking the "Test" button prints the value "Changed: yes" to the console, while React renders "no":
yes_no_ref

Note: this works as expected if I remove the <StrictMode> wrapper in index.js.

Link to code example: https://codesandbox.io/s/youthful-jang-2xw3i?file=/src/App.js

The current behavior

"Changed: yes" in printed to console, but "Changed: no" is rendered by React.

The expected behavior

"Changed: yes" in printed to console, "Changed: yes" is rendered by React.

@Yakimych Yakimych added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Aug 15, 2021
@Yakimych Yakimych changed the title Bug: Value printed to console is rendered incorrectly by React Bug: Value printed to console is rendered incorrectly in StrictMode Aug 15, 2021
@bvaughn
Copy link
Contributor

bvaughn commented Aug 16, 2021

Closing this as a duplicate of #21783. See #21889 for new our new plans here (already implemented as PR #22030)

@bvaughn bvaughn closed this as completed Aug 16, 2021
@aminick
Copy link

aminick commented Oct 15, 2021

Thanks for reporting, this thread has saved my sanity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

3 participants