Get value of input text with react-bootstrap
Asked Answered
W

2

5

I try to get value into a input text and add it to a text area with react-bootstrap.

I know I must use ReactDOM.findDOMNode to get value with ref. I don't understand what is wrong.

Here my code :

import React from 'react';
import logo from './logo.svg';
import ReactDOM from 'react-dom';
import { InputGroup, FormGroup, FormControl, Button} from 'react-bootstrap';
import './App.css';
class InputMessages extends React.Component {
constructor(props) { 
super(props);
this.handleChange =      this.handleChange.bind(this); 
    this.GetMessage= this.GetMessage.bind(this); 
this.state = {message: ''};
}   
handleChange(event)
{    
this.setState({message: this.GetMessage.value});
}
GetMessage()
{   
return ReactDOM.findDOMNode(this.refs.message     );
 }
 render() {
    var message = this.state.message;
    return(
 <FormGroup > 
 <FormControl
 componentClass="textarea" value={message} />
 <InputGroup> 
 <FormControl type="text" ref='message' /> 
    <InputGroup.Button>
    <Button bsStyle="primary" onClick={this.handleChange}>Send
    </Button>
    </InputGroup.Button> 
    </InputGroup>
    </FormGroup>
    );
   }
   }  
   export default InputMessages;
Warthog answered 19/7, 2017 at 15:6 Comment(1)
When asking for help, taking the time to format your code reasonably and consistency is likely to help you get better/faster answers. Also, consider updating your question with a runnable minimal reproducible example using Stack Snippets (the [<>] toolbar button). Stack Snippets support React, including JSX; here's how to do one.Canonist
L
3

Add an Input ref to your form :

<FormControl inputRef={ref => { this.myInput = ref; }} />

so now you get the value like

this.myInput.value
Lightening answered 19/7, 2017 at 15:27 Comment(2)
It works, but I store values in state. I would like to store it into variable. I use 2 components, so I can not write this.myinput.value into other componentWarthog
<FormControl > componentClass="textarea" value={this.input.value} /> don't recognize the ref, whereas the function doWarthog
S
6

Form Control has a ref prop, which allows us to use React Refs

Sample Code :

class MyComponent extends React.Component {
  constructor() {
     /* 1. Initialize Ref */
     this.textInput = React.createRef(); 
  }

  handleChange() {
     /* 3. Get Ref Value here (or anywhere in the code!) */
     const value = this.textInput.current.value;
  }

  render() {
    /* 2. Attach Ref to FormControl component */
    return (
      <div>
        <FormControl ref={this.textInput} type="text" onChange={() => this.handleChange()} />
      </div>
    )
  }
}

Hope this helps!

Sandrocottus answered 21/3, 2019 at 7:30 Comment(1)
Very helpful! Thank you!!Thibaut
L
3

Add an Input ref to your form :

<FormControl inputRef={ref => { this.myInput = ref; }} />

so now you get the value like

this.myInput.value
Lightening answered 19/7, 2017 at 15:27 Comment(2)
It works, but I store values in state. I would like to store it into variable. I use 2 components, so I can not write this.myinput.value into other componentWarthog
<FormControl > componentClass="textarea" value={this.input.value} /> don't recognize the ref, whereas the function doWarthog

© 2022 - 2024 — McMap. All rights reserved.