mui / mui/material-ui

[material-ui][AvatarGroup] Even with null results the avatar is still counted

Open
#40,118 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

scope: avatar type: enhancement
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Duplicates
  • I have searched the existing issues
Latest version
  • I have tested the latest version
Steps to reproduce 🕹
  • Use the AvatarGroup component and loop an array of avatars inside.
  • Don't directly do the condition inside the AvatarGroup, instead, render a child component and pass the current avatar data.
  • On the Child component, create a condition to either render the avatar or return null instead, so that the avatar won't be rendered.

Here is the Live Demo: SANDBOX-MATERIALUI-AVATAR-GROUP

Current behavior 😯

In some cases, I need to do the condition inside the child component to either render the avatar or not after fetching some data by using a hook which needs to be on the top level.

However, returning null to not render the avatar is still being counted by the avatargroup causing it to still render the + icon.

Expected behavior 🤔

Do not count the null avatars that are being returned so that the + icon will not be rendered.

Context 🔦

This makes the component look a bit buggy a it renders the + icon even without avatars. I'm not sure if this is a limitation in which we should not do it this way or if there is another approach we should be doing instead. Thank you!

Your environment 🌎
npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

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

Start with the AvatarGroup implementation and the linked CodeSandbox demo at demo.tsx:57,44; inspect how child components that return null are counted. Done means null-rendering children are excluded from the count so the + indicator is absent when no visible avatars remain, with the behavior verified against the reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.