ReactJs: Prevent multiple times button press

前端 未结 9 996
别跟我提以往
别跟我提以往 2020-11-27 13:21

In my React component I have a button meant to send some data over AJAX when clicked. I need to happen only the first time, i.e. to disable the button after it\'s first use.

9条回答
  •  天命终不由人
    2020-11-27 13:44

    By using event.target , you can disabled the clicked button. Use arrow function when you create and call the function onClick. Don't forget to pass the event in parameter.

    See my codePen

    Here is the code:

    class Buttons extends React.Component{
      constructor(props){
        super(props)
        this.buttons = ['A','B','C','D']
      }
    
      disableOnclick = (e) =>{
        e.target.disabled = true
      }
    
      render(){
        return(
    
         
    {this.buttons.map((btn,index) => ( ))}
    )} } ReactDOM.render(, document.body);

提交回复
热议问题