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

160
Views
react More Collapse and Expand

At first, I want to make only 5 data.text values ​​visible and all data.text to be exposed when the More button is clicked. Then I want to only 5 data.text visible when the close button is pressed.

However, in the code I wrote, at first, five are visible, but when the More button is clicked, it doesn't work properly.

How do I modify the code to make it work?

   const [visible, setVisible] = useState(5)
   const [expanded, setexpanded ] = useState(false);

      const showMore = () => {
        visible === 5 ? (
            setVisible({ visible: ProductDetail.allergies.length, expanded: true })
         
        ) : (
            setVisible({ visible: 5, expanded: false })
        )
    }

  return (
     
     <ul>
         {ProductDetail && ProductDetail.allergies?.length ? 
                            ProductDetail.allergies.slice(0, 
                           visibleBlogs).map(All => (
                            <li key={All.id}>
                             .....
                             </li>
    </ul>

                    <button type="button" onClick={showMore}>
                        {expanded ? (
                            <span>close</span>
                        ) : (
                            <span>more</span>
                        )}  
                    </button>
                    

                    
)

about 4 years ago · Juan Pablo Isaza
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!