Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an input value using "refs" in react-bootstrap form?

I'm trying to create a form that appears in modal. So when user input a value, that value is stored in local storage. Here's a picture that help's you to understand what I mean:
enter image description here Here is the code of the form:

function FieldGroup({id, label, help, ...props}) {     return (         <ReactBootstrap.FormGroup controlId={id}>             <ReactBootstrap.ControlLabel>{label}</ReactBootstrap.ControlLabel>             <ReactBootstrap.FormControl {...props} />             {help && <ReactBootstrap.HelpBlock>{help}</ReactBootstrap.HelpBlock>}         </ReactBootstrap.FormGroup>     ); }  const formInstance = (     <form>         <FieldGroup             id="formControlsText"             type="text"             label="Text"             placeholder="Recipe Name"             inputRef={ref => { this.input = ref; }}         />         <ReactBootstrap.FormGroup controlId="formControlsTextarea">             <ReactBootstrap.ControlLabel>Ingredients</ReactBootstrap.ControlLabel>             <ReactBootstrap.FormControl componentClass="textarea" placeholder="Enter Ingredients" />         </ReactBootstrap.FormGroup>     </form> );   

As I've read in bootstrap React tutorial, I should add
<FormControl inputRef={ref => { this.input = ref; }} /> to the FormControl props. But after adding it I get an error when modal form is invoked:

`enter image description here

like image 985
Taras Yaremkiv Avatar asked Dec 11 '16 10:12

Taras Yaremkiv


People also ask

How do I use REF in bootstrap react?

Get Select Element Value Using RefThe first step is to create ref in the React component. Now, the next step is to implement the react-bootstrap select element with the added ref property. There is the added property ref along with the select element followed by the name of reference you created before.


2 Answers

I just made this issue. My code:

<FormControl     componentClass="input"     placeholder="Enter recipe title"     inputRef={(ref) => {this.input = ref}}     defaultValue={title}/> </FormGroup> 

And then you can get the value from <FormControl> in some handler like this:

console.log(this.input.value); 

Details can be found in my repo: https://github.com/kerf007/recipebook

like image 170
Eugene Kulakov Avatar answered Oct 14 '22 00:10

Eugene Kulakov


I have same problem with you, and this is my solution

const FieldGroup = ({id, label, help, inputRef, ...props}) =>   <FormGroup controlId={id}>     <ControlLabel>{label}</ControlLabel>     <FormControl {...props} inputRef={inputRef}/>     {help && <HelpBlock>{help}</HelpBlock>}   </FormGroup> 

and my form

<form>     <FieldGroup         id="bookName"         type="text"         label="Name"         placeholder="Enter name..."         inputRef = {(input) => this.inputName = input }      />     <FieldGroup         id="bookAuthor"         label="Author"         type="text"         placeholder="author 's name..."         inputRef={(input) => this.inputAuthor = input}      /> </form> 

then you can get book 's name and author 's name value by:

this.inputName.value and this.inputAuthor.value 
like image 38
M.Tae Avatar answered Oct 14 '22 00:10

M.Tae