mobxjs / mobx

Simple, scalable state management.

Home Page:http://mobx.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

dmitrytavern opened this issue · comments

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 you are right, I replaced getServerSnapshot with adm.getSnapshot and the double rendering didn't happen. Should I create a PR with getServerSnapshot replaced?

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.