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

116
Vistas
Make a sepecific button load in JSON state react using setState

I have 3 buttons which are rendered from a loop over an array and rendered in react.

The 3 buttons are represented in a state as follows:

const [buttonLoading, setButtonLoading] = UseSafeState({
    free: { is_loading: false },
    starter: { is_loading: false },
    professional: { is_loading: false },
})

(UseSafeState is just a hook component that do exactly the same thing as useState, but safely)

I would like that by clicking on one of the buttons, a loader is displayed on this specific button while waiting for the end of a request.

I created this test component to shorten my code and test. What I'm trying to do here is display the loader on the button that is clicked relative to its name.

export default function Test() {
    const [buttonLoading, setButtonLoading] = UseSafeState({
        free: { is_loading: false },
        starter: { is_loading: false },
        professional: { is_loading: false },
    })
    
    // To set the is_loading property of the specific key of the json state
    const changeButtonLoading = (type, isLoading) => {
        const buttonLoadingChanged = buttonLoading
        buttonLoadingChanged[type].is_loading = isLoading

        setButtonLoading(buttonLoadingChanged)
    }

    const BUTTONS = [
        { name: 'free', label: 'FREE' },
        { name: 'starter', label: 'STARTER' },
        { name: 'professional', label: 'PROFESIONAL' },
    ]

    return BUTTONS.map((button, key) => {
        return (
            <Button key={key} onClick={() => changeButtonLoading(button.name, true)}>
                {buttonLoading[button.name].is_loading ? <LoaderButton /> : button.label}
            </Button>
        )
    })
}

(LoaderButton /> component is just a basic loader with some css and html)

But the problem is that when I click on one of the button, he don't display the loader. But he display it if I modify something in my code to force a render.

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

0

const changeButtonLoading = (type, isLoading) => {
  setButtonLoading(prev => {
    return {
      ...prev,
      [type] : {is_loading : isLoading}
     }
  })
};
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