Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React stateless component this.refs..value?

Tags:

reactjs

I don't know if I'm doing this correctly... If I want to get value from an input I use this.refs.whatever.value.trim() but if that input is a stateless function component how do I do retrieve the value onSubmit?

I know this isn't correct now after researching but how are you supposed to get value from these input fields?

import React, {Component} from 'react'  import {InputField} from '../components/forms/InputField' import {Button} from '../components/forms/Button'  export default class SignupWrapper extends Component {   _handleSubmit(e) {     e.preventDefault();     const email = this.refs.email.value.trim();     const password = this.refs.password.value.trim();     const confirm = this.refs.confirm.value.trim();     console.log({email, password, confirm});   }    render() {     return (       <form id="application-signup" onSubmit={this._handleSubmit.bind(this)}>         <InputField type={'email'} name={'email'} text={'email'}                     helpBlock={'email is required'} ref="email" />         <InputField type={'password'} name={'password'} text={'password'}                     helpBlock={'password is required'} ref="password" />         <InputField type={'password'} name={'confirm'} text={'confirm password'}                     helpBlock={'password confirmation is required'} ref="confirm" />         <Button type={'submit'} className={'btn btn-primary'} text={'signup'} />       </form>     )   } } 

this is the stateless inputfield

import React from 'react'  export const InputField = (props) => (   <div className="form-group col-xs-12">     <label htmlFor={props.name}>{props.text}</label>     <input type={props.type} name={props.name} className="form-control"            data-stripe={props.stripe} />     <span className="help-block">{props.helpBlock}</span>   </div> ) 
like image 607
cocacrave Avatar asked May 17 '16 03:05

cocacrave


People also ask

How is ref used in stateless components?

The idea of stateless is that there isn't an instance created for it (state). As such, you can't attach a ref , since there's no state to attach the ref to. Your best bet would be to pass in a callback for when the component changes and then assign that text to the parent's state.

How do you give ref to the functional component React?

To create a ref in a functional component we use the useRef() hook which returns a mutable object with a . current property set to the initialValue we passed to the hook. This returned object will persist for the full lifetime of the component. Thus, throughout all of its re-rendering and until it unmounts.

Can we use ref on React component?

In order to get a reference to a React component, you can either use this to get the current React component, or you can use a ref to get a reference to a component you own. They work like this: var MyComponent = React. createClass({ handleClick: function() { // Explicitly focus the text input using the raw DOM API.


1 Answers

You can use refs inside stateless components.

Here is also my example fiddle that shows you how it works.

import React from 'react'  export default ({ onChange }) => {   let cityInput    const onSubmit = e => {     e.preventDefault()     onChange(cityInput.value)   }    return (     <form onSubmit={ onSubmit }>       <input type='text' placeholder='Enter City Name'         ref={ el => cityInput = el } />       <button>Go!</button>     </form>   ) } 

Note: Although this is one way to do it, however, this approach is not recommended unless you really need it. Try to think about more on redesigning your React code instead of hacking it like this. You may read more about it here.

like image 118
Inanc Gumus Avatar answered Sep 22 '22 23:09

Inanc Gumus