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

229
Vistas
Make button clickable after state changes

So I have this count where I will increment once the button with handleFollow() is clicked (i have multiple buttons with handleFollow() and I'm only showing one here). How do keep track of the count and then change the allowNext to false once the count is more than 3 so that I can make the button clickable.

let count = 0;
const [allowNext, setAllowNext] = useState(true)
const [followed, setFollowed] = useState({
    1 : false,
    2 : false,
    3 : false,
    4 : false,
    5 : false,
  })

const handleFollow = (idx) => {
    const val = !followed[idx]
    setFollowed(prev => ({...prev, [idx] : val}))
    count += 1
}

<div>
     <Button onClick={() => {handleFollow(5);}} style={buttonStyle1} disabled={clicked[4]}>Continue</Button> 
</div>

// disabled={allowNext=false} only when count is more than 3 to make it clickable
<div>
     <Button style={buttonStyle2} disabled={allowNext} onClick={() => {setSelectedForm(2)}}>Next page</Button>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You define count as state first, and then you can add useEffect for this case

const [count, setCount] = useState(0);

useEffect(() => {
    if (count > 3){ 
      setAllowNext(false);
    }

}, [count]);

And change the way you increment in handleFolow instead of count += 1, use setCount(prev => prev+1)

Alternative approach would be, keep count as state and keep setCount increment, but instead of useEffect, after you increment count you directly check

if (count > 3){ 
  setAllowNext(false) 
}

However I am not sure if this is safe because we access directly after the changing the state, bug might occur, so first approach would be my way to go

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