How to disable button in React.js

后端 未结 8 1786
迷失自我
迷失自我 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: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 (
              
    this.input=input} onChange = {this.change.bind(this)} />
    ); } } ReactDOM.render( , document.getElementById('root'));
    
    
    

提交回复
热议问题