Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

   export class BusinessDetailsForm extends Component {       submitForm(data) {         console.log(this.refs.googleInput.value)         }       }       reder() {         return(           <form onSubmit={this.submitForm}>             <Field type="text"               name="location"               component={GoogleAutoComplete}               id="addressSearchBoxField"               ref="googleInput"             />           </form>         )       }     } 
like image 458
Ashh Avatar asked Mar 31 '17 09:03

Ashh


People also ask

What are refs How will you use it for getting input value explain with code?

Refs are a function provided by React to access the DOM element and the React element that you might have created on your own. They are used in cases where we want to change the value of a child component, without making use of props and all.

How do you use REF IN input tag?

The ref is created in the constructor and then attached to the input element when it renders. When the button is clicked, the value submitted from the input element (which has the ref attached) is used to update the state of the text (contained in an H3 tag). We make use of this. textInput.

How do you get values from form in React?

To get input values on form submit in React:Store the values of the input fields in state variables. Set the onSubmit prop on the form element. Access the values of the input fields in your handleSubmit function.


2 Answers

You should avoid ref="googleInput" as it is now considered legacy. You should instead declare

ref={(googleInput) => { this.googleInput = googleInput }} 

Inside of your handler, you can use this.googleInput to reference the element.

Then inside of your submitForm function, you can obtain the text value with this.googleInput._getText()

String refs are legacy https://facebook.github.io/react/docs/refs-and-the-dom.html

If you worked with React before, you might be familiar with an older API where the ref attribute is a string, like "textInput", and the DOM node is accessed as this.refs.textInput. We advise against it because string refs have some issues, are considered legacy, and are likely to be removed in one of the future releases. If you're currently using this.refs.textInput to access refs, we recommend the callback pattern instead.

Edit

From React 16.3, the format for creating refs are:

class Component extends React.Component  {         constructor()          {             this.googleInput = React.createRef();         }          render()          {             return              (                 <div ref={this.googleInput}>                     {/* Details */}                 </div>             );         }     } 
like image 65
Dan Avatar answered Oct 04 '22 04:10

Dan


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

like image 28
mohitSehgal Avatar answered Oct 04 '22 03:10

mohitSehgal