How to disable button in React.js

后端 未结 8 1749
迷失自我
迷失自我 2020-12-01 04:06

I have this component:

import React from \'react\';

export default class AddItem extends React.Component {

add() {
    this.props.onButtonClick(this.input.         


        
相关标签:
8条回答
  • 2020-12-01 04:33

    I have had a similar problem, turns out we don't need hooks to do these, we can make an conditional render and it will still work fine.

    <Button
        type="submit"
        disabled={
            name === "" || email === "" || password === "" ? true : false
        }
        fullWidth
        variant="contained"
        color="primary"
        className={classes.submit}>
        SignUP
    </Button>
    
    0 讨论(0)
  • 2020-12-01 04:35

    this.input is undefined until the ref callback is called. Try setting this.input to some initial value in your constructor.

    From the React docs on refs, emphasis mine:

    the callback will be executed immediately after the component is mounted or unmounted

    0 讨论(0)
  • 2020-12-01 04:35

    very simple solution for this is by using useRef hook

    const buttonRef = useRef();
    
    const disableButton = () =>{
      buttonRef.current.disabled = true; // this disables the button
     }
    
    <button
    className="btn btn-primary mt-2"
    ref={buttonRef}
    onClick={disableButton}
    >
        Add
    </button>
    

    Similarly you can enable the button by using buttonRef.current.disabled = false

    0 讨论(0)
  • 2020-12-01 04:40

    In HTML,

    <button disabled/>
    <buttton disabled="true">
    <buttton disabled="false">
    <buttton disabled="21">
    

    All of them boils down to disabled="true" that is because it returns true for a non-empty string. Hence, in order to return false, pass a empty string in a conditional statement like this.input.value?"true":"".

    render() {
        return (
            <div className="add-item">
                <input type="text" className="add-item__input" ref={(input) => this.input = input} placeholder={this.props.placeholder} />
                <button disabled={this.input.value?"true":""} className="add-item__button" onClick={this.add.bind(this)}>Add</button>
            </div>
        );
    }
    
    0 讨论(0)
  • 2020-12-01 04:42

    just Add:

    <button disabled={this.input.value?"true":""} className="add-item__button" onClick={this.add.bind(this)}>Add</button>
    
    0 讨论(0)
  • 2020-12-01 04:45

    There are few typical methods how we control components render in React.

    But, I haven't used any of these in here, I just used the ref's to namespace underlying children to the component.

    class AddItem extends React.Component {
        change(e) {
          if ("" != e.target.value) {
            this.button.disabled = false;
          } else {
            this.button.disabled = true;
          }
        }
    
        add(e) {
          console.log(this.input.value);
          this.input.value = '';
          this.button.disabled = true;
        }
    
        render() {
            return (
              <div className="add-item">
              <input type="text" className = "add-item__input" ref = {(input) => this.input=input} onChange = {this.change.bind(this)} />
              
              <button className="add-item__button" 
              onClick= {this.add.bind(this)} 
              ref={(button) => this.button=button}>Add
              </button>
              </div>
            );
        }
    }
    
    ReactDOM.render(<AddItem / > , document.getElementById('root'));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <div id="root"></div>

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