Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Modifying Textarea Values

Tags:

reactjs

I am working on a project which is basically notepad. I am having problems though updating the <textarea>'s value when an ajax call is made. I tried setting the textarea's value property but then no changes to its value can be made. How can I make it so on a state change the textarea's value changes and can be edited.

The code I have is as follows.

In the parent class

<Editor name={this.state.fileData} /> 

In the Editor class

var Editor = React.createClass({ render: function() { return (     <form id="noter-save-form" method="POST">     <textarea id="noter-text-area" name="textarea" value={this.props.name}></textarea>     <input type="submit" value="Save" />     </form> ); } 

});

I can't use defaultValue because the value of the textarea is not known on page load and when I try and put the data between the textareas nothing happens. I would like it to take the state value whenever the state changes but have it editable in between.

Thanks

Edit

I managed to get it working using jQuery but would like to do it in React instead, I called this before render:

$('#noter-text-area').val(this.props.name); 
like image 641
phlie Avatar asked Oct 20 '15 19:10

phlie


People also ask

How get textarea value in React?

To get the value of a textarea in React, set the onChange prop on the textarea field and access its value as event. target. value or use a ref for an uncontrolled textarea element and access its value as ref. current.

How do I change the input value in React?

To get the value of an input on button click in React: Declare a state variable that tracks the value of the input field. Add an onClick prop to a button element. When the button is clicked, update the state variable.

Does textarea have onChange?

Description. The onchange property of a Textarea element refers to an event handler function that is invoked when the user changes the value in the text area and then “commits” those changes by moving keyboard focus elsewhere.


1 Answers

I think you want something along the line of:

Parent:

<Editor name={this.state.fileData} /> 

Editor:

var Editor = React.createClass({   displayName: 'Editor',   propTypes: {     name: React.PropTypes.string.isRequired   },   getInitialState: function() {      return {       value: this.props.name     };   },   handleChange: function(event) {     this.setState({value: event.target.value});   },   render: function() {     return (       <form id="noter-save-form" method="POST">         <textarea id="noter-text-area" name="textarea" value={this.state.value} onChange={this.handleChange} />         <input type="submit" value="Save" />       </form>     );   } }); 

This is basically a direct copy of the example provided on https://facebook.github.io/react/docs/forms.html

Update for React 16.8:

import React, { useState } from 'react';  const Editor = (props) => {     const [value, setValue] = useState(props.name);      const handleChange = (event) => {         setValue(event.target.value);     };      return (         <form id="noter-save-form" method="POST">             <textarea id="noter-text-area" name="textarea" value={value} onChange={handleChange} />             <input type="submit" value="Save" />         </form>     ); }  Editor.propTypes = {     name: PropTypes.string.isRequired }; 
like image 61
Matthew Herbst Avatar answered Sep 19 '22 11:09

Matthew Herbst