mobxjs / mobxjs/mobx

forceUpdate bug on observer components: "Warning: Cannot update a component (`X`) while rendering a different component (`X`)"

Open
#3,752 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

🐛 bug
Dominant language
TypeScript
Stars
28.2k
Forks
1.8k
Avg merge
1h 38m
Merged PRs (30d)
1

Description

Intended outcome:
observer components should not force a re-render during a render

Actual outcome:
observer components are forcing re-render during a render

How to reproduce the issue:
I'm trying to integrate react-query and mobx using fromResource. Everything works fine except, when 2 different components consuming the same resource are mounted, the second once logs a warning (bad setState call warning). It is hard to explain throughly here but I created a public repo that showcases this pretty simply.

clone this repo: https://github.com/hasanayan/mobx-react-query

  • npm install
  • npm run dev
  • visit the app
  • click on one of the toggle buttons once
  • wait until the resouece is loaded (you'll see "Cool Entity" on screen)
  • click on the other toggle button once
  • check console, you will see the warning

Versions

"mobx": "^6.10.2",
"mobx-react": "^9.0.1",
"mobx-utils": "^6.0.8",
"@tanstack/react-query": "5.0.0-beta.20" (I believe this is irrelevant)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Clone the linked hasanayan/mobx-react-query repository, run npm install and npm run dev, then follow the toggle sequence to reproduce the warning. Start by tracing the observer components and shared fromResource usage involved in the reproduction. Done means the second component can mount without logging a state-update-during-render warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.