ReactJs: Prevent multiple times button press

前端 未结 9 935
别跟我提以往
别跟我提以往 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 (
          <button
            type='submit'
            onClick={() => this.uploadFile()}
            disabled={this.state.isButtonDisabled}>
            Upload
          </button>
        )
      }
    }
    
    ReactDOM.render(<UploadArea />, document.body);
    
    0 讨论(0)
  • 2020-11-27 13:56

    You can try using React Hooks to set the Component State.

    import React, { useState } from 'react';
    
    const Button = () => {
      const [double, setDouble] = useState(false);
      return (
        <button
          disabled={double}
          onClick={() => {
            // doSomething();
            setDouble(true);
          }}
        />
      );
    };
    
    export default Button;
    

    Make sure you are using ^16.7.0-alpha.x version of react and react-dom.

    Hope this helps you!

    0 讨论(0)
  • 2020-11-27 13:59

    If you disable the button during onClick, you basically get this. A clean way of doing this would be:

    import React, { useState } from 'react';
    import Button from '@material-ui/core/Button';
    
    export default function CalmButton(props) {
        const [executing, setExecuting] = useState(false);
    
        const {
            disabled,
            onClick,
            ...otherProps
        } = props;
    
        const onRealClick = async (event) => {
            setExecuting(true);
            try {
                await onClick();
            } finally {
                setExecuting(false);
            }
        };
    
        return (
            <Button
                onClick={onRealClick}
                disabled={executing || disabled}
                {...otherProps}
            />
        )
    }
    
    

    See it in action here: https://codesandbox.io/s/extended-button-that-disabled-itself-during-onclick-execution-mg6z8

    We basically extend the Button component with the extra behaviour of being disabled during onClick execution. Steps to do this:

    1. Create local state to capture if we are executing
    2. Extract properties we tamper with (disabled, onClick)
    3. Extend onClick operation with setting the execution state
    4. Render the button with our overridden onClick, and extended disabled

    NOTE: You should ensure that the original onClick operation is async aka it is returning a Promise.

    0 讨论(0)
提交回复
热议问题