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

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

    In case any one is wondering how to implement ref with hooks :

    // Import
    import React, { useRef } from 'react';
    
    
    const Component = () => {
        // Create Refs
        const exemploInput = useRef();
    
        const handleSubmit = (e) => {
            e.preventDefault();
       
             const inputTest = exampleInput.current.value;
    
         }
    
        return(
            <form onSubmit={handleSubmit}>
                <label>
                    Name:
                    <input type="text" ref={input} />
                </label>
                <input type="submit" value="Submit" />
            </form>
    }
    
    0 讨论(0)
  • 2021-02-03 21:39

    using ref={ inputRef => this.input = inputRef } is considered legacy now. In React 16.3 onwards, you can use the code below,

    class MyForm extends React.Component {
        constructor(props) {
            //...
            this.input = React.createRef();
        }
    
        handleSubmit(event) {
            alert('A name was submitted: ' + this.input.current.value);
            event.preventDefault();
        }
    
        render() {
            return (
                <form onSubmit={this.handleSubmit}>
                    <label>
                        Name:
                        <input type="text" ref={this.input} />
                    </label>
                    <input type="submit" value="Submit" />
                </form>
            );
        }
    }
    

    EDIT: thanks for the comment @stormwild

    0 讨论(0)
  • 2021-02-03 21:42

    Using RN 0.57.8 when tried this.googleInput._getText(), It resulted in error _getText is not a function so i printed this.googleInput in console and found that _getText() is a function inside _root

    1. this.googleInput._root._getText()
    2. this.googleInput._root._lastNativeText - This will return the last state not the current state please be careful while using it.
    0 讨论(0)
  • 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 (
                <div>
                    <input
                        className="form-control name"
                        ref={ n => this.name = n }
                        type="text"
                    />
    
                    <button className="btn btn-warning" onClick={ this.handleClick }>Search</button>
                </div>
            );
        }
    }
    
    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 (
                    <div>
                        <input
                            className="form-control name"
                            ref={this.name}
                            type="text"
                        />
    
                        <button className="btn btn-warning" onClick={ this.handleClick }>Search</button>
                    </div>
                );
            }
        }
    
        export default Search;
    

    Hope it will help you.

    0 讨论(0)
  • 2021-02-03 21:45

    The react docu explains it very well: https://reactjs.org/docs/refs-and-the-dom.html

    this is considered legacy:

    yourHandleMethod() {
      this.googleInput.click();
    };
    
    yourRenderCode(){
      ref={(googleInput) => { this.googleInput = googleInput }}
    };
    

    whereas, this is considered the way to go:

    constructor(props){
      this.googleInput = React.createRef();
    };
    yourHandleMethod() {
      this.googleInput.current.click();
    };
    yourRenderCode(){
      <yourHTMLElement
        ref={this.googleInput}
      />
    };
    
    0 讨论(0)
  • 2021-02-03 21:46

    I tried the answer above (https://stackoverflow.com/a/52269988/1978448) and found it only worked for me when I put the refs in the state, but not when I just made them properties of the component.

    Constructor:

    this.state.refs={
      fieldName1: React.createRef(),
      fieldName2: React.createRef()
    };
    

    and in my handleSubmit I create a payload object to post to my server like this:

    var payload = {
      fieldName1: this.state.refs.fieldName1.current.value,
      fieldName2: this.state.refs.fieldName2.current.value,
    }
    
    0 讨论(0)
提交回复
热议问题