Skip to content

fix: defer useSelector notify during render to avoid "Cannot update a component" react error - #638

Merged
jmeistrich merged 9 commits into
mainfrom
fix/update-observable-during-react-render
Jul 12, 2026
Merged

fix: defer useSelector notify during render to avoid "Cannot update a component" react error#638
jmeistrich merged 9 commits into
mainfrom
fix/update-observable-during-react-render

Conversation

@DorianMazur

@DorianMazur DorianMazur commented Feb 18, 2026

Copy link
Copy Markdown
Collaborator

Setting an observable inside a useSelector callback during render causes React to error: "Cannot update a component while rendering a different component". This is because notify (which tells React to re-render) fires synchronously while another component is still rendering.

Fix: Defer notify to a microtask. Added a test that checks the warning no longer appears.

Related: #144

@DorianMazur DorianMazur changed the title fix: defer useSelector notify during render to avoid react error fix: defer useSelector notify during render to avoid "Cannot update a component" react error Feb 18, 2026
@DorianMazur
DorianMazur marked this pull request as ready for review February 25, 2026 13:07
@DorianMazur
DorianMazur requested a review from Copilot February 25, 2026 13:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a React warning triggered when an observable is updated during another component’s render by deferring useSelector’s notification to a microtask, and updates the React test suite accordingly.

Changes:

  • Defer useSelector store notifications via queueMicrotask to avoid “Cannot update a component while rendering a different component”.
  • Update many React tests to use async act to accommodate deferred notifications.
  • Add regression tests ensuring the “Cannot update a component” warning no longer appears in key scenarios.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
src/react/useSelector.ts Defers notify calls to a microtask to avoid React mid-render update warnings.
tests/testglobals.ts Makes supressActWarning async and awaits the provided callback.
tests/reactive-observer.test.tsx Converts tests/act usage to async patterns.
tests/react.test.tsx Converts many tests to async act, adds new regression tests for the warning, and adjusts tracing tests.
Comments suppressed due to low confidence (1)

tests/react.test.tsx:603

  • This test currently defines an async () => { ... } function but never invokes it, so none of the assertions/run logic executes and the test will always pass. Remove the extra arrow function wrapper (and consider giving the test a unique name since it duplicates the previous one).
    test('use$ with array length', async () => {
        async () => {
            const obs$ = observable<{ test: number[] }>({
                test: [0],
            });
            let lastValue: number[] | undefined = undefined;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/react.test.tsx Outdated
Comment thread tests/testglobals.ts Outdated
Comment thread src/react/useSelector.ts
Comment thread src/react/useSelector.ts Outdated
@LegendApp LegendApp deleted a comment from Copilot AI Mar 2, 2026
@jmeistrich
jmeistrich force-pushed the fix/update-observable-during-react-render branch from 867f8bf to 8ed7c4d Compare July 12, 2026 11:31
@jmeistrich
jmeistrich merged commit 49220e0 into main Jul 12, 2026
6 checks passed
@jmeistrich
jmeistrich deleted the fix/update-observable-during-react-render branch July 12, 2026 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants