0

I have this piece of code but can't work out how I can disable the button after Pay is clicked:

        } else if (paymentMethod.name === "Pay by Cash") {
            return (
                <Tab.Pane eventKey={paymentMethod.id} key={key}>
                    <h6 className="mb-3 mt-0 mb-3">Cash</h6>
                    {this.state.paymentMethodSelectionError && this.state.paymentMethodSelectionError !== "" && <Alert key="error_div" variant="danger">{this.state.paymentMethodSelectionError}</Alert>}
                    <p>You are paying by cash</p>
                    <Form.Group className="mb-0">
                        <Button className="btn btn-success btn-block btn-lg"
                            onClick={() => {
                                this.setState({
                                    selectedPaymentMethod: "not-charged",
                                    paymentGateway: paymentMethod.id
                                }, () => {
                                    this.setPaymentMethod()
                                })
                            }}>
                            PAY £{this.state.totalPay}<Icofont icon="long-arrow-right" /></Button>
                    </Form.Group>
                </Tab.Pane>)

Any help is appreciated

3 Answers 3

3

You can put a state for disabling your button:

this.state = {
  disabled: false
}

In click function, change it to true:

const clickButton = () => {
  this.setState({ disabled: true });
}

Then change your HTML to something like this:

<button type="button" disabled={ this.state.disabled }>Click Me!</button>

Preview: Here

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

2 Comments

Sorry I am a newbie on React, could you show an example using the code above
@Rutnet I added a link.
0
  1. Declare a new state variable to take care of the disabled button:

const [buttonState,setButtonState]=useState(false)

  1. Attach a onClick handler to disable the button and also manage the disabled property of the button.

<button onClick={()=>{ setButtonState(true)}} disabled={buttonState}> PAY </button>

Comments

0

You can use state to control the button state

Preview for both Functional and Class components

Codesandbox Preview Link

Comments

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.