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

123
Visualizações
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 Respostas
Responde à pergunta

0

const changeButtonLoading = (type, isLoading) => {
  setButtonLoading(prev => {
    return {
      ...prev,
      [type] : {is_loading : isLoading}
     }
  })
};
about 4 years ago · Juan Pablo Isaza Relatório
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