1

I have around 10 similar components on the page with different names. Each component is drawn by for loop and have some buttons and data.

If I click one button on a component it carries out an API call and buttons becomes disable till the the call is in success.

The issue is if I click another button on another component button becomes disable(which is expected) but enables the previously clicked button( not expected) as the setState is changed. How can I make the button only enable when its in their own success state.

That is why I am adding the button names in the state of clickedRunButton making it an object but How to add the items in this object during the API call and remove it during the success of the call.

Instead of adding another item its overwriting it.

    this.state = {
        clickedRunButton:{ pipeName :''}
    }

    onClickRun(params) {        
    const url = `/api/${params}/run`;
    let clickedRunButton = Object.assign({}, 
        this.state.clickedRunButton);    //creating copy of object
    return $.ajax({
        type: 'POST',
        url,
        processData: false,
        contentType: 'application/json',
        beforeSend :() =>{
            clickedRunButton.pipeName = params;  
            clickedRunButton[pipeName] = "run"+params;
            this.setState({
                clickedRunButton
            })
        },

        success: (response) => { 
            if(!response.error) {
                clickedRunButton.pipeName = params;  
                clickedRunButton[pipeName] = '';
                this.setState({
                clickedRunButton
               })
            }
        },

        error: (error) => {
        }
    });
}

1 Answer 1

1

Take a look at:

clickedRunButton.pipeName = params;  

and

clickedRunButton[pipeName] = "run"+params;

pipeName used in clickedRunButton[pipeName] is not defined. This will be causing problems.

You can assign a variable to an index in an object like so:

clickedRunButton[clickedRunButton.pipeName] = "run"+params

Hope this helps

Sign up to request clarification or add additional context in comments.

5 Comments

Oh actually. What I want to do is create an object where the key is pipeline name and value is "run"+pipeline name. The pipeline name will be params
How to make the key in this.state as params ??
@user3594118 Ahh, I will updat
wow this works. What to do if I have to clear the value ? like in one method i have to set the value back to ''. If I do this in setState it gives me error. ` _getPipelineStatus = (pipelineName) => { fetch(/api/ndp/v1/admin/pipelines/${pipelineName}) .then(res => res.json()) .then( (result) => { this.setState({ clickedRestartButton[pipelineName] : '' }); } ) }`
just set the entire object: this.setState({clickedRunButton})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.