ReactJs: Prevent multiple times button press

前端 未结 9 983
别跟我提以往
别跟我提以往 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:51

    Tested as working one: http://codepen.io/zvona/pen/KVbVPQ

    class UploadArea extends React.Component {
      constructor(props) {
        super(props)
    
        this.state = {
          isButtonDisabled: false
        }
      }
    
      uploadFile() {
        // first set the isButtonDisabled to true
        this.setState({
          isButtonDisabled: true
        });
        // then do your thing
      }
    
      render() {
        return (
          
        )
      }
    }
    
    ReactDOM.render(, document.body);
    

提交回复
热议问题