Skip to content

[material-ui][Badge] Fix stale values being used when badge is invisible #43083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

DiegoAndai
Copy link
Member

Fixes: #43081

To see the error:

  • Go to the before/after demo
  • Inspect the .MuiBadge-badge element
  • Change the value back and forth between 8 and 0
  • Observe the .MuiBadge-badge element's value

Before: https://stackblitz.com/edit/react-qkjzbj?file=Demo.tsx
After: Waiting for CI build

I think the use of usePreviousProps is incorrect, as badgeContent, max and displayValue shouldn't be coupled with the invisible value. They should always be updated regardless. It was originally added for another use case: mnajdova@7572c90#diff-26877d58ee4ebaa271700aae39d84f0ce250e8d01c677024bbf0d53f46159b1b, so it might've stuck around inadvertently.

I fixed in both mui-material and mui-base for consistency.

@DiegoAndai DiegoAndai self-assigned this Jul 26, 2024
@DiegoAndai DiegoAndai added component: badge This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material labels Jul 26, 2024
@mui-bot
Copy link

mui-bot commented Jul 26, 2024

Netlify deploy preview

https://deploy-preview-43083--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 6a98b91

@@ -134,7 +134,7 @@ describe('<Badge />', () => {
it('should render with invisible class when invisible and showZero are set to false and content is 0', () => {
const { container } = render(<Badge badgeContent={0} showZero={false} invisible={false} />);
expect(findBadge(container)).to.have.class(classes.invisible);
expect(findBadge(container)).to.have.text('');
expect(findBadge(container)).to.have.text('0');
Copy link
Member

Choose a reason for hiding this comment

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

This reverts the fix introduced in #21557. This fix has added the usage of the usePreviousProps hook. You will noticed on this demo: https://next.mui.com/material-ui/react-badge/#badge-visibility, that when clicking on the "-" button, when the value is 1, it immediately disappears without adding the "0" as a content - this is a non valid state. The issue is again here on this PR deploy preview.

@mnajdova
Copy link
Member

I think the use of usePreviousProps is incorrect, as badgeContent, max and displayValue shouldn't be coupled with the invisible value. They should always be updated regardless. It was originally added for another use case: mnajdova@7572c90#diff-26877d58ee4ebaa271700aae39d84f0ce250e8d01c677024bbf0d53f46159b1b, so it might've stuck around inadvertently.

I would recommend when going trough history to search for the PR that did the fix, as it is usually how you can understand the motivation for the code change, e.g. #21557 has a description & explanation. Also, changing a test should be an indication that something will be broken/behavior will change.

@DiegoAndai DiegoAndai added the on hold There is a blocker, we need to wait label Dec 16, 2024
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: badge This is the name of the generic UI component, not the React module! on hold There is a blocker, we need to wait package: material-ui Specific to @mui/material PR: out-of-date The pull request has merge conflicts and can't be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[material-ui][Badge] When showZero={false} and value is 0 content is not updated
3 participants