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

前端 未结 11 981
耶瑟儿~
耶瑟儿~ 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:43

    From React 16.2, you can use: React.createRef

    See more: https://reactjs.org/docs/refs-and-the-dom.html

    1. using ref={ inputRef => this.input = inputRef }

    Exam:

    import React, { Component } from 'react';
    
    class Search extends Component {
        constructor(props) {
            super(props);
    
            this.name = React.createRef();
    
            this.handleClick = this.handleClick.bind(this);
        }
    
        handleClick() {
            this.props.onSearch(`name=${this.name.value}`);
        }
    
        render() {
            return (
                
    this.name = n } type="text" />
    ); } } export default Search;

    ref={ n => this.name = n } Use Callback Refs -> see

    Or:

    2. this.name.current.focusTextInput()

        class Search extends Component {
            constructor(props) {
                super(props);
    
                this.name = React.createRef();
    
                this.handleClick = this.handleClick.bind(this);
            }
    
            handleClick() {
                this.props.onSearch(`name=${this.name.current.value}`);
            }
    
            render() {
                return (
                    
    ); } } export default Search;

    Hope it will help you.

提交回复
热议问题