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

mobx-react-lite component wrapped in observer renders twice in latest Next.js #3826

Closed
dmitrytavern opened this issue Feb 6, 2024 · 3 comments · Fixed by #3830 or #3813
Closed

mobx-react-lite component wrapped in observer renders twice in latest Next.js #3826

dmitrytavern opened this issue Feb 6, 2024 · 3 comments · Fixed by #3830 or #3813
Labels

Comments

@dmitrytavern
Copy link
Contributor

Intended outcome:

Render a component wrapped in observer once.

Actual outcome:

Component wrapped in observer made two renders.

How to reproduce the issue:
Repro:

  1. Create next application
  2. Install latest mobx and mobx-react-lite
  3. Set reactStrictMode: false in next.config.js
  4. Add Child component to Index page
import { enableStaticRendering, observer } from "mobx-react-lite";

enableStaticRendering(typeof window === "undefined");

const ChildObserver = observer(function Child() {
  console.log("Hello world");

  return <div>Test</div>;
});

export default function Index() {
  return <ChildObserver />;
}

Result:

image

If downgrade mobx-react-lite to 3.4.3:

image

I also tried to reproduce this bug with a pure react application, but everything works fine there (with 4.0.5 and 3.4.3 versions).

If build and run the application, the bug remains.

Versions
next@14.1.0
mobx-react-lite@4.0.5

@urugator
Copy link
Collaborator

urugator commented Feb 13, 2024

@dmitrytavern
Copy link
Contributor Author

@urugator you are right, I replaced getServerSnapshot with adm.getSnapshot and the double rendering didn't happen. Should I create a PR with getServerSnapshot replaced?

@urugator
Copy link
Collaborator

Yes, PR is welcome, thank you. Don't forget to provide changeset via yarn changeset. Dunno if there is an easy way to write a unit test for this.

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