1

I am trying to auto-disable a button after the ID has hit a certain number, say 7. The codes compiled but unable to execute, i.e. disable button based on the condition. Using React and Tailwind.

    <div className="mt-8 mb-16">
      <button
        id="BTS"
        onClick={handlePurchase}
        type="button"
        disabled={this.id >= 7 }
        className="inline-flex items-center ... "
      >
        Mint me a Marköbot!
      </button>
    </div>

Source code for the app is at (line 103)

Thanks lots!

Posted the question with an alt approach at How to merge two buttons into one?

Mark Tan
  • 21
  • 1
  • 6

2 Answers2

1

Managed to solve this. Found out the handleClick part is missing, which is required to render 'this' inline. Thanks to all who have come forth with suggestions. Do share if you have a simpler solution.

class Toggle extends React.Component {
  constructor(props) {
    super(props);
    this.id = 0;
    this.handleClick = this.handleClick.bind(this);
  }

handleClick() {
  this.setState(prevState => ({
    isToggleOn: !prevState.isToggleOn
  }));
}

render() {
  return (
    <button onClick={this.handleClick}
      disabled={this.id <= 9 ? true : false}>
        {this.id <= 9 ? 'X' : 'Y'}
    </button>
  );
}
}

ReactDOM.render(
  <Toggle />,
  document.getElementById('root')
);
Mark Tan
  • 21
  • 1
  • 6
0

With your code, is this.id means button id? Why didn't you use additional state for conditional disabling?

I've see your code, you use function component, and I assume this.id is the button id

You can create new state and make use that state as condition, such as

    const [buttonId, setButtonId] = React.useState(null)

    return(
        <button
          id="BTS"
          onClick={handlePurchase}
          type="button"
          disabled={buttonId >= 7 }
          className="inline-flex items-center ... "
        >
          Mint me a Marköbot!
        </button>
    )
  • Hi Ega. Thanks for the response. this.id refers to the NFT ID that is currently being minted by the smart contract. I'm looking for a way to disable the button once the NFT (indexed by ID on the smart contract) hits a certain number (which will depend on the number of NFTs generated through the contract). The app output is at botdapp.netlify.app. – Mark Tan Jul 21 '21 at 07:36
  • Anyway to make this inline code within work? .... disabled={this.id <= 6 ? false : true} .... – Mark Tan Jul 21 '21 at 08:39
  • I think it didn't need the lambda expression to return false and true, because the `this.id <= 6` already return boolean – Ega Rifqi Saputra Jul 21 '21 at 09:32