How to get values from input types using this.refs in reactjs?

前端 未结 11 1209
耶瑟儿~
耶瑟儿~ 2021-02-03 21:23

Not able to get values of input type using this.refs... how to get that values from input type

   export class BusinessDetailsForm extends Component {
      subm         


        
11条回答
  •  青春惊慌失措
    2021-02-03 21:49

    I think the more idiomatic way is to use state instead of refs, although it's a little more code in this case since you only have a single input.

    export class BusinessDetailsForm extends Component {
    
      constructor(props) {
        super(props);
        this.state = { googleInput: '' };
        this.defaultValue = 'someValue';
        this.handleChange = this.handleChange.bind(this);
        this.submitForm = this.submitForm.bind(this);
      }
    
      handleChange(e) {
        const { field, value } = e.target;
        this.setState({ [field]: value });
      }
      submitForm() {
        console.log(this.state.googleInput);
      }
      render() {
        return (
          
            
          
        );
      }
    }
    

    See https://facebook.github.io/react/docs/forms.html#controlled-components.

提交回复
热议问题