Skip to content

fix(connect): log mapStateToProps errors to console.error in dev (#1942) - #2306

Merged
timdorr merged 6 commits into
reduxjs:masterfrom
webdevelopersrinu:fix/1942-log-mapstate-errors-in-dev
Jun 29, 2026
Merged

timdorr merged 6 commits into
reduxjs:masterfrom
webdevelopersrinu:fix/1942-log-mapstate-errors-in-dev

Conversation

@webdevelopersrinu

Copy link
Copy Markdown
Contributor

Summary

Logs errors thrown in mapStateToProps (or selectors) to console.error
in non-production environments, so they surface during development
instead of being silently swallowed.

Fixes #1942

Background

Since v8.0.0, errors thrown inside mapStateToProps are caught in the
subscription callback (connect.tsx#L133-L136) and stored in
lastThrownError. In v7, those errors surfaced because the component
unmounted on selector errors (the unmount path re-throws
lastThrownError). In v8+, the component no longer unmounts — it just
re-renders in its pre-error state — so the error is silently swallowed.

This was diagnosed by @alexanderchr in
#1942 (Dec 12, 2024), and @markerikson approved the dev-only
logging approach in the same thread:

"logging this in dev seems like a passable fallback"

Changes

  • src/components/connect.tsx — log the caught error to console.error
    when process.env.NODE_ENV !== 'production', with a brief message and
    a link back to this issue
  • test/components/connect.spec.tsx — add a unit test that dispatches
    an action which causes mapStateToProps to throw, then asserts the
    error was logged via console.error

Behavior

  • ✅ Production: unchanged (the new branch is dead-code-eliminated)
  • ✅ Development: error is now logged to the console with the
    original error object, making it discoverable in DevTools

Test plan

  • yarn test — all connect tests pass (65/65), new test passes,
    no regressions in code I touched
  • yarn lint runs as part of pretest and passes
  • Type-check passes (vitest --typecheck reports no errors)

…uxjs#1942)

Errors thrown inside mapStateToProps (or selectors) are caught in the
subscription callback at connect.tsx and stored in lastThrownError, but
since v8 the component no longer unmounts on selector errors so the
captured error is silently swallowed during development.

This adds a console.error log gated by process.env.NODE_ENV !== 'production'
so the error surfaces in dev tools, aiding debugging without changing any
production behavior.

Diagnosed by @alexanderchr on the issue thread; approach approved by
@markerikson in Dec 2024.

Fixes reduxjs#1942
@codesandbox-ci

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@aryaemami59 aryaemami59 changed the title fix(connect): log mapStateToProps errors to console.error in dev (#1942) fix(connect): log mapStateToProps errors to console.error in dev (#1942) May 20, 2026
@aryaemami59

aryaemami59 commented May 20, 2026 •

Copy link
Copy Markdown
Member

The current CI failure is related to arethetypeswrong/arethetypeswrong.github.io#258 which is caused by 101arrowz/fflate#258.

As a temporary workaround, we can pin fflate to 0.8.2 via resolutions in package.json:

  "resolutions": {
    "fflate": "0.8.2"
  },

This should unblock CI until the upstream issue is resolved.

@webdevelopersrinu

Copy link
Copy Markdown
Contributor Author

Thanks @aryaemami59 — totally missed your earlier comment, sorry about
that. Happy to add the fflate resolutions pin to this PR if you'd
prefer, or wait if you're patching master directly. Just let me know.

@aryaemami59

aryaemami59 commented May 21, 2026 •

Copy link
Copy Markdown
Member

@webdevelopersrinu

Thanks @aryaemami59 — totally missed your earlier comment, sorry about that. Happy to add the fflate resolutions pin to this PR if you'd prefer, or wait if you're patching master directly. Just let me know.

Thanks. I'd prefer to wait and see whether this gets resolved upstream first. That also gives the rest of the team more time to review the PR.

Update: This has since been fixed upstream in @arethetypeswrong/[email protected], so the fflate resolutions pin isn't needed here. I've opened #2307 to bump @arethetypeswrong/cli to 0.18.3 and unblock CI.

@pkg-pr-new

pkg-pr-new Bot commented Jun 28, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/react-redux@0a6bd13 -D
yarn add https://pkg.pr.new/[email protected] -D
pnpm add https://pkg.pr.new/[email protected] -D
bun add https://pkg.pr.new/[email protected] -D

commit: 0a6bd13

@aryaemami59 aryaemami59 changed the title fix(connect): log mapStateToProps errors to console.error in dev (#1942) fix(connect): log mapStateToProps errors to console.error in dev (#1942) Jun 28, 2026
@aryaemami59 aryaemami59 added this to the 10.0 milestone Jun 28, 2026

@aryaemami59 aryaemami59 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two small follow-ups on top of the dev-logging change:

  1. Name the component that threw — the message currently says "a connect()-ed component", which isn't actionable. displayName (Connect(X)) is already computed in _connect and in scope at the call site, so threading it through is cheap and matches the existing mapStateToProps() in Connect(X) … style.
  2. Make the test pin NODE_ENV — it's titled "…in development" but never sets the env; it passes only because the runner isn't production.

Comment thread src/components/connect.tsx Resolved
Comment thread src/components/connect.tsx Resolved
Comment thread src/components/connect.tsx Resolved
Comment thread test/components/connect.spec.tsx Resolved
Comment thread test/components/connect.spec.tsx Resolved
…or log

Per review on reduxjs#1942: thread `displayName` into the dev console.error so it
identifies which `connect()`-ed component threw, and harden the test to set
NODE_ENV='development' explicitly and assert the component name is logged.

Refs reduxjs#1942
@webdevelopersrinu

Copy link
Copy Markdown
Contributor Author

@aryaemami59 thanks for the review! Addressed all the feedback in 048188c:

  • Threaded displayName through subscribeUpdates and included it in the
    dev console.error message, so it now names the offending component
    (e.g. "Connect(Container)"). Used the double-quoted style to match the
    existing "Could not find store" error in this file.
  • Updated the test to explicitly set NODE_ENV='development' (and restore
    it), and to assert the logged message names the component.

Let me know if you'd like anything else.

Comment thread test/components/connect.spec.tsx Resolved
Comment thread test/components/connect.spec.tsx Resolved
@webdevelopersrinu

Copy link
Copy Markdown
Contributor Author

Thanks for the review @aryaemami59

@timdorr

timdorr commented Jun 29, 2026

Copy link
Copy Markdown
Member

Thanks!

@timdorr
timdorr merged commit b74ae3a into reduxjs:master Jun 29, 2026
39 checks passed
kkennethlau12393 pushed a commit to kkennethlau12393/reactjs__react-redux that referenced this pull request Aug 31, 2026
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.

connect swallows errors in mapStateToProps (new bug in 8.0.0)

3 participants