Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda