Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
React js hide rows when too many elements in a list

I'm displaying a list of users with avatars horizontally, when there are many of them or the display is small a new row appears. Now if the third row appears I want to hide it and display show more button and only 2 rows should be visible.

Can someone help me with that? I know how to achieve it with only text but here the user component includes an avatar, role, and username because of that I can't use a line_height and calculate a number of rows.

<ul>{data.map(item => <li style={{display: "inline-block"}}><DisplayUser user={item}/></li>)}</ul>

UserComponent

      <UserData>
        {user.avatarUrl && (
          <Avatar
            sx={{ marginRight: "10px", width: 42, height: 42 }}
            src={user.avatarUrl}
          />
        )}
        <Box
          sx={{
            display: "flex",
            flexDirection: "column",
            alignItems: "flex-start",
          }}
        >
          <Typography variant="body1" sx={{ lineHeight: "inherit" }} noWrap>
            {user.userName}
          </Typography>
          <Typography
            variant="caption"
            sx={{ color: "var(--text-secondary)" }}
            noWrap
          >
            {user.role}
          </Typography>
        </Box>
      </UserData>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use state.num instead of line-height to save the number of items you will render, and increase it when the button is cilcked.

Increase function (Function component)

const [num, setNum] = useState(0)
const increaseNum = () => {
  setNum(num + 2)
}

Render

data.map((item, index) => {
  if (index > num) { return false } // end loop
  return (
    <UserData>
      ...
    </UserData>
  )
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!