Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
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 answers
Answer question

0

const changeButtonLoading = (type, isLoading) => {
  setButtonLoading(prev => {
    return {
      ...prev,
      [type] : {is_loading : isLoading}
     }
  })
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!