Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
React create more/close on button click Type Error

I made a function that prints 5 data at a time and prints all data.length when the More button is pressed.

But there is a problem. Every time I click the button I get an error.

To illustrate my code, I first create a visible state and only display 5 at first. 'expanded' say 'true all product calls' away when clicked on a 'button' `false show only the first 5'

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

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

  return (

 {ProductDetail && ProductDetail.allergies?.length ? 
                            ProductDetail.allergies.slice(0, visible).map(All => (
                            <li key={All.id}>
         {All.displayText}</li>
                        )) : <li>No information.</li>}
)


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

    }

But every time I click the button I get an error.

TypeError: setVisible(...) is not a function

  46 | 
  47 |    
  48 |    const showMore = () => {
> 49 |        visible === 5 ? (
  50 |          setVisible(ProductDetail.allergies.length)
  51 |        ) (
  52 |           setexpanded(true)

I don't know how to solve it. How would you like to modify the code? Oh, and I want to make the buttons invisible if there are less than 5 at first, but I don't know how to write the code.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda