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?

2
  • Usually disabled attribute has value as disabled Commented Jul 21, 2021 at 3:11
  • what's not working? Commented Jul 21, 2021 at 3:17

2 Answers 2

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') ); 
Sign up to request clarification or add additional context in comments.

Comments

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> ) 

3 Comments

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.
Anyway to make this inline code within <button /> work? .... disabled={this.id <= 6 ? false : true} ....
I think it didn't need the lambda expression to return false and true, because the this.id <= 6 already return boolean

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.