Skip to content

[useButton] Capture component stack for nativeButton error message - #3861

Merged
atomiks merged 2 commits into
mui:masterfrom
atomiks:fix/button-capture-component-stack
Jan 29, 2026
Merged

atomiks merged 2 commits into
mui:masterfrom
atomiks:fix/button-capture-component-stack

Conversation

@atomiks

@atomiks atomiks commented Jan 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #3779, at least for React 19.1+ (when React.captureOwnerStack() was released). React 18 is supposedly better at printing the component stack trace inside effects already, though.

Before (+ expanded):
Screenshot 2026-01-26 at 9 13 40 am

After (collapsed):
Screenshot 2026-01-26 at 9 12 51 am

⚠️ You need to refresh the page to see the stack from first load(?)

@atomiks atomiks added component: button Changes related to the button component. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. labels Jan 25, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jan 25, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@3861
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@3861
    

commit: 3100199

@mui-bot

mui-bot commented Jan 25, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jan 25, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 3100199
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/697bd892bb557f00084bcf06
😎 Deploy Preview https://deploy-preview-3861--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks force-pushed the fix/button-capture-component-stack branch 4 times, most recently from 75b4dca to 9cf4584 Compare January 25, 2026 22:34
@atomiks
atomiks marked this pull request as ready for review January 25, 2026 22:34
@greptile-apps

greptile-apps Bot commented Jan 25, 2026

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

Enhances the developer experience by capturing React component stack traces in nativeButton error messages. Uses React.captureOwnerStack() (available in React 19.1+) to append component hierarchy information to error messages, making it easier to identify which component triggered the warning. Test assertions were updated to use expect.stringContaining() to accommodate the dynamic component stack suffix.

  • Imports SafeReact utility and calls captureOwnerStack?.() with optional chaining for backward compatibility
  • Appends stack trace to both error messages in useButton validation effect
  • Updates 5 test assertions across 2 test files to use partial string matching

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • The changes are minimal, well-tested, and enhance developer experience without affecting runtime behavior. The use of optional chaining ensures backward compatibility with older React versions, and all tests were properly updated to accommodate the new dynamic error message format.
  • No files require special attention

Important Files Changed

Filename Overview
packages/react/src/use-button/useButton.ts Added component stack capture to error messages for better debugging in React 19.1+
packages/react/src/use-button/useButton.test.tsx Updated test assertions to use expect.stringContaining() to accommodate dynamic component stack in error messages
packages/react/src/toolbar/button/ToolbarButton.test.tsx Updated test assertions to use expect.stringContaining() for error message validation

@atomiks
atomiks force-pushed the fix/button-capture-component-stack branch from 4bad03e to 3100199 Compare January 29, 2026 22:00
@atomiks
atomiks merged commit ad517c7 into mui:master Jan 29, 2026
@atomiks
atomiks deleted the fix/button-capture-component-stack branch January 29, 2026 22:05
const ownerStackMessage = SafeReact.captureOwnerStack?.() || '';
error(
'A component that acts as a button was not rendered as a native <button>, which does not match the default. Ensure that the element passed to the `render` prop of the component is a real <button>, or set the `nativeButton` prop on the component to `false`.',
`A component that acts as a button was not rendered as a native <button>, which does not match the default. Ensure that the element passed to the \`render\` prop of the component is a real <button>, or set the \`nativeButton\` prop on the component to \`false\`.${ownerStackMessage}`,

@oliviertassinari oliviertassinari Jan 31, 2026 •

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.

Would this make more sense? I'm struggling to understand what "which does not match the default" mean.

Suggested change
`A component that acts as a button was not rendered as a native <button>, which does not match the default. Ensure that the element passed to the \`render\` prop of the component is a real <button>, or set the \`nativeButton\` prop on the component to \`false\`.${ownerStackMessage}`,
`A component that acts as a button was not rendered as a native <button>. For the component to behave correctly, it needs the `nativeButton` boolean prop to match the actual DOM node rendered. To fix this, ensure that the element passed to the \`render\` prop of the component is a real <button>, or set the \`nativeButton\` prop on the component to \`false\`.${ownerStackMessage}`,

matching:

  1. Say what’s the problem
  2. Say why it's a problem
  3. Point out how to solve the problem

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

"Default" means the "default rendered element tag", so e.g. a component's JSDoc says "Renders a <button> element."

Yeah, the error message is missing the second dot point (it's needed for SSR), but handles the first and third.

@oliviertassinari

Copy link
Copy Markdown
Member

Nice 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: button Changes related to the button component. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[utils] Improve error message for render prop

4 participants