Skip to content

Commit

Permalink
Merge pull request #24318 from s-alves10/fix/issue-24062
Browse files Browse the repository at this point in the history
fix: sync avatarRows and props.icons using useMemo
  • Loading branch information
techievivek authored Aug 10, 2023
2 parents 5ea37aa + 3f8f843 commit ff423aa
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/components/MultipleAvatars.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {memo, useEffect, useState} from 'react';
import React, {memo, useMemo} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import _ from 'underscore';
Expand Down Expand Up @@ -91,17 +91,15 @@ function getContainerStyles(size, isInReportAction) {
return containerStyles;
}
function MultipleAvatars(props) {
const [avatarRows, setAvatarRows] = useState([props.icons]);
let avatarContainerStyles = getContainerStyles(props.size, props.isInReportAction);
const singleAvatarStyle = props.size === CONST.AVATAR_SIZE.SMALL ? styles.singleAvatarSmall : styles.singleAvatar;
const secondAvatarStyles = [props.size === CONST.AVATAR_SIZE.SMALL ? styles.secondAvatarSmall : styles.secondAvatar, ...props.secondAvatarStyle];
const tooltipTexts = props.shouldShowTooltip ? _.pluck(props.icons, 'name') : [''];

const calculateAvatarRows = () => {
const avatarRows = useMemo(() => {
// If we're not displaying avatars in rows or the number of icons is less than or equal to the max avatars in a row, return a single row
if (!props.shouldDisplayAvatarsInRows || props.icons.length <= props.maxAvatarsInRow) {
setAvatarRows([props.icons]);
return;
return [props.icons];
}

// Calculate the size of each row
Expand All @@ -112,14 +110,7 @@ function MultipleAvatars(props) {
const secondRow = props.icons.slice(0, rowSize);

// Update the state with the two rows as an array
setAvatarRows([firstRow, secondRow]);
};

useEffect(() => {
calculateAvatarRows();

// The only dependencies of the effect are based on props, so we can safely disable the exhaustive-deps rule
// eslint-disable-next-line react-hooks/exhaustive-deps
return [firstRow, secondRow];
}, [props.icons, props.maxAvatarsInRow, props.shouldDisplayAvatarsInRows]);

if (!props.icons.length) {
Expand Down

0 comments on commit ff423aa

Please sign in to comment.