React Modifying Textarea Values
up vote
12
down vote
favorite
I am working on a project which is basically notepad. I am having problems though updating the '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);
reactjs
add a comment |
up vote
12
down vote
favorite
I am working on a project which is basically notepad. I am having problems though updating the '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);
reactjs
Are you looking for onChange, from the React docs? facebook.github.io/react/docs/forms.html#controlled-components
– Hypaethral
Oct 20 '15 at 19:22
On change is for when the value of the textarea changes. I don't really need that just want the textarea to be updated upon a state change of its parent class.
– phlie
Oct 20 '15 at 19:27
Are you sure the state of the parent is actually changing?
– Hypaethral
Oct 20 '15 at 19:28
I used a alert(this.state.fileData) to test and it seems to be. I also tried <textarea>{this.props.name}</textarea> but that doesn't work. If I change the value property it changes but then is uneditable.
– phlie
Oct 20 '15 at 19:31
add a comment |
up vote
12
down vote
favorite
up vote
12
down vote
favorite
I am working on a project which is basically notepad. I am having problems though updating the '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);
reactjs
I am working on a project which is basically notepad. I am having problems though updating the '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);
reactjs
reactjs
edited Oct 20 '15 at 19:36
asked Oct 20 '15 at 19:14
phlie
3361412
3361412
Are you looking for onChange, from the React docs? facebook.github.io/react/docs/forms.html#controlled-components
– Hypaethral
Oct 20 '15 at 19:22
On change is for when the value of the textarea changes. I don't really need that just want the textarea to be updated upon a state change of its parent class.
– phlie
Oct 20 '15 at 19:27
Are you sure the state of the parent is actually changing?
– Hypaethral
Oct 20 '15 at 19:28
I used a alert(this.state.fileData) to test and it seems to be. I also tried <textarea>{this.props.name}</textarea> but that doesn't work. If I change the value property it changes but then is uneditable.
– phlie
Oct 20 '15 at 19:31
add a comment |
Are you looking for onChange, from the React docs? facebook.github.io/react/docs/forms.html#controlled-components
– Hypaethral
Oct 20 '15 at 19:22
On change is for when the value of the textarea changes. I don't really need that just want the textarea to be updated upon a state change of its parent class.
– phlie
Oct 20 '15 at 19:27
Are you sure the state of the parent is actually changing?
– Hypaethral
Oct 20 '15 at 19:28
I used a alert(this.state.fileData) to test and it seems to be. I also tried <textarea>{this.props.name}</textarea> but that doesn't work. If I change the value property it changes but then is uneditable.
– phlie
Oct 20 '15 at 19:31
Are you looking for onChange, from the React docs? facebook.github.io/react/docs/forms.html#controlled-components
– Hypaethral
Oct 20 '15 at 19:22
Are you looking for onChange, from the React docs? facebook.github.io/react/docs/forms.html#controlled-components
– Hypaethral
Oct 20 '15 at 19:22
On change is for when the value of the textarea changes. I don't really need that just want the textarea to be updated upon a state change of its parent class.
– phlie
Oct 20 '15 at 19:27
On change is for when the value of the textarea changes. I don't really need that just want the textarea to be updated upon a state change of its parent class.
– phlie
Oct 20 '15 at 19:27
Are you sure the state of the parent is actually changing?
– Hypaethral
Oct 20 '15 at 19:28
Are you sure the state of the parent is actually changing?
– Hypaethral
Oct 20 '15 at 19:28
I used a alert(this.state.fileData) to test and it seems to be. I also tried <textarea>{this.props.name}</textarea> but that doesn't work. If I change the value property it changes but then is uneditable.
– phlie
Oct 20 '15 at 19:31
I used a alert(this.state.fileData) to test and it seems to be. I also tried <textarea>{this.props.name}</textarea> but that doesn't work. If I change the value property it changes but then is uneditable.
– phlie
Oct 20 '15 at 19:31
add a comment |
2 Answers
2
active
oldest
votes
up vote
24
down vote
accepted
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}></textarea>
<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
Thanks, and thanks for the link.
– phlie
Oct 20 '15 at 19:50
but text is not editable in the textarea. How to resolve this ? @phlie
– Bhanu prathap
Apr 14 '17 at 5:26
@Bhanuprathap it should be - what problem are you having?
– Matthew Herbst
Apr 14 '17 at 5:34
React component will get a XML from backend and store it in state and passed to textarea through value prop .Programatically alter the xml and somtimes user also modify it. Now on passing through text area value property, user cannot able to modify the xml. @MatthewHerbst
– Bhanu prathap
Apr 17 '17 at 11:13
2
@Bhanuprathap sounds like you should ask a new question for your problem and post your code. You can link to this question in that question for reference.
– Matthew Herbst
Apr 17 '17 at 22:09
|
show 1 more comment
up vote
0
down vote
As a newbie in React world, I came across a similar issues where I could not edit
the textarea and struggled
with binding. It's worth knowing about controlled
and uncontrolled
elements when it comes to react.
The value of the following uncontrolled textarea
cannot be changed because of value
<textarea type="text" value="some value"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following uncontrolled textarea
can be changed because of use of defaultValue
or no value attribute
<textarea type="text" defaultValue="sample"
onChange={(event) => this.handleOnChange(event)}></textarea>
<textarea type="text"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following controlled textarea
can be changed because of how
value is mapped to a state as well as the onChange
event listener
<textarea value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
Here is my solution using different syntax. I prefer the auto-bind
than manual binding however, if I were to not use {(event) => this.onXXXX(event)}
then that would cause the content of textarea
to be not editable OR the event.preventDefault()
does not work as expected. Still a lot to learn I suppose.
class Editor extends React.Component {
constructor(props) {
super(props)
this.state = {
textareaValue: ''
}
}
handleOnChange(event) {
this.setState({
textareaValue: event.target.value
})
}
handleOnSubmit(event) {
event.preventDefault();
this.setState({
textareaValue: this.state.textareaValue + ' [Saved on ' + (new Date()).toLocaleString() + ']'
})
}
render() {
return <div>
<form onSubmit={(event) => this.handleOnSubmit(event)}>
<textarea rows={10} cols={30} value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
<br/>
<input type="submit" value="Save"/>
</form>
</div>
}
}
ReactDOM.render(<Editor />, document.getElementById("content"));
The versions of libraries are
"babel-cli": "6.24.1",
"babel-preset-react": "6.24.1"
"React & ReactDOM v15.5.4"
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
24
down vote
accepted
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}></textarea>
<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
Thanks, and thanks for the link.
– phlie
Oct 20 '15 at 19:50
but text is not editable in the textarea. How to resolve this ? @phlie
– Bhanu prathap
Apr 14 '17 at 5:26
@Bhanuprathap it should be - what problem are you having?
– Matthew Herbst
Apr 14 '17 at 5:34
React component will get a XML from backend and store it in state and passed to textarea through value prop .Programatically alter the xml and somtimes user also modify it. Now on passing through text area value property, user cannot able to modify the xml. @MatthewHerbst
– Bhanu prathap
Apr 17 '17 at 11:13
2
@Bhanuprathap sounds like you should ask a new question for your problem and post your code. You can link to this question in that question for reference.
– Matthew Herbst
Apr 17 '17 at 22:09
|
show 1 more comment
up vote
24
down vote
accepted
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}></textarea>
<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
Thanks, and thanks for the link.
– phlie
Oct 20 '15 at 19:50
but text is not editable in the textarea. How to resolve this ? @phlie
– Bhanu prathap
Apr 14 '17 at 5:26
@Bhanuprathap it should be - what problem are you having?
– Matthew Herbst
Apr 14 '17 at 5:34
React component will get a XML from backend and store it in state and passed to textarea through value prop .Programatically alter the xml and somtimes user also modify it. Now on passing through text area value property, user cannot able to modify the xml. @MatthewHerbst
– Bhanu prathap
Apr 17 '17 at 11:13
2
@Bhanuprathap sounds like you should ask a new question for your problem and post your code. You can link to this question in that question for reference.
– Matthew Herbst
Apr 17 '17 at 22:09
|
show 1 more comment
up vote
24
down vote
accepted
up vote
24
down vote
accepted
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}></textarea>
<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
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}></textarea>
<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
edited Oct 20 '15 at 20:31
Hypaethral
1,025921
1,025921
answered Oct 20 '15 at 19:45
Matthew Herbst
9,559134284
9,559134284
Thanks, and thanks for the link.
– phlie
Oct 20 '15 at 19:50
but text is not editable in the textarea. How to resolve this ? @phlie
– Bhanu prathap
Apr 14 '17 at 5:26
@Bhanuprathap it should be - what problem are you having?
– Matthew Herbst
Apr 14 '17 at 5:34
React component will get a XML from backend and store it in state and passed to textarea through value prop .Programatically alter the xml and somtimes user also modify it. Now on passing through text area value property, user cannot able to modify the xml. @MatthewHerbst
– Bhanu prathap
Apr 17 '17 at 11:13
2
@Bhanuprathap sounds like you should ask a new question for your problem and post your code. You can link to this question in that question for reference.
– Matthew Herbst
Apr 17 '17 at 22:09
|
show 1 more comment
Thanks, and thanks for the link.
– phlie
Oct 20 '15 at 19:50
but text is not editable in the textarea. How to resolve this ? @phlie
– Bhanu prathap
Apr 14 '17 at 5:26
@Bhanuprathap it should be - what problem are you having?
– Matthew Herbst
Apr 14 '17 at 5:34
React component will get a XML from backend and store it in state and passed to textarea through value prop .Programatically alter the xml and somtimes user also modify it. Now on passing through text area value property, user cannot able to modify the xml. @MatthewHerbst
– Bhanu prathap
Apr 17 '17 at 11:13
2
@Bhanuprathap sounds like you should ask a new question for your problem and post your code. You can link to this question in that question for reference.
– Matthew Herbst
Apr 17 '17 at 22:09
Thanks, and thanks for the link.
– phlie
Oct 20 '15 at 19:50
Thanks, and thanks for the link.
– phlie
Oct 20 '15 at 19:50
but text is not editable in the textarea. How to resolve this ? @phlie
– Bhanu prathap
Apr 14 '17 at 5:26
but text is not editable in the textarea. How to resolve this ? @phlie
– Bhanu prathap
Apr 14 '17 at 5:26
@Bhanuprathap it should be - what problem are you having?
– Matthew Herbst
Apr 14 '17 at 5:34
@Bhanuprathap it should be - what problem are you having?
– Matthew Herbst
Apr 14 '17 at 5:34
React component will get a XML from backend and store it in state and passed to textarea through value prop .Programatically alter the xml and somtimes user also modify it. Now on passing through text area value property, user cannot able to modify the xml. @MatthewHerbst
– Bhanu prathap
Apr 17 '17 at 11:13
React component will get a XML from backend and store it in state and passed to textarea through value prop .Programatically alter the xml and somtimes user also modify it. Now on passing through text area value property, user cannot able to modify the xml. @MatthewHerbst
– Bhanu prathap
Apr 17 '17 at 11:13
2
2
@Bhanuprathap sounds like you should ask a new question for your problem and post your code. You can link to this question in that question for reference.
– Matthew Herbst
Apr 17 '17 at 22:09
@Bhanuprathap sounds like you should ask a new question for your problem and post your code. You can link to this question in that question for reference.
– Matthew Herbst
Apr 17 '17 at 22:09
|
show 1 more comment
up vote
0
down vote
As a newbie in React world, I came across a similar issues where I could not edit
the textarea and struggled
with binding. It's worth knowing about controlled
and uncontrolled
elements when it comes to react.
The value of the following uncontrolled textarea
cannot be changed because of value
<textarea type="text" value="some value"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following uncontrolled textarea
can be changed because of use of defaultValue
or no value attribute
<textarea type="text" defaultValue="sample"
onChange={(event) => this.handleOnChange(event)}></textarea>
<textarea type="text"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following controlled textarea
can be changed because of how
value is mapped to a state as well as the onChange
event listener
<textarea value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
Here is my solution using different syntax. I prefer the auto-bind
than manual binding however, if I were to not use {(event) => this.onXXXX(event)}
then that would cause the content of textarea
to be not editable OR the event.preventDefault()
does not work as expected. Still a lot to learn I suppose.
class Editor extends React.Component {
constructor(props) {
super(props)
this.state = {
textareaValue: ''
}
}
handleOnChange(event) {
this.setState({
textareaValue: event.target.value
})
}
handleOnSubmit(event) {
event.preventDefault();
this.setState({
textareaValue: this.state.textareaValue + ' [Saved on ' + (new Date()).toLocaleString() + ']'
})
}
render() {
return <div>
<form onSubmit={(event) => this.handleOnSubmit(event)}>
<textarea rows={10} cols={30} value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
<br/>
<input type="submit" value="Save"/>
</form>
</div>
}
}
ReactDOM.render(<Editor />, document.getElementById("content"));
The versions of libraries are
"babel-cli": "6.24.1",
"babel-preset-react": "6.24.1"
"React & ReactDOM v15.5.4"
add a comment |
up vote
0
down vote
As a newbie in React world, I came across a similar issues where I could not edit
the textarea and struggled
with binding. It's worth knowing about controlled
and uncontrolled
elements when it comes to react.
The value of the following uncontrolled textarea
cannot be changed because of value
<textarea type="text" value="some value"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following uncontrolled textarea
can be changed because of use of defaultValue
or no value attribute
<textarea type="text" defaultValue="sample"
onChange={(event) => this.handleOnChange(event)}></textarea>
<textarea type="text"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following controlled textarea
can be changed because of how
value is mapped to a state as well as the onChange
event listener
<textarea value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
Here is my solution using different syntax. I prefer the auto-bind
than manual binding however, if I were to not use {(event) => this.onXXXX(event)}
then that would cause the content of textarea
to be not editable OR the event.preventDefault()
does not work as expected. Still a lot to learn I suppose.
class Editor extends React.Component {
constructor(props) {
super(props)
this.state = {
textareaValue: ''
}
}
handleOnChange(event) {
this.setState({
textareaValue: event.target.value
})
}
handleOnSubmit(event) {
event.preventDefault();
this.setState({
textareaValue: this.state.textareaValue + ' [Saved on ' + (new Date()).toLocaleString() + ']'
})
}
render() {
return <div>
<form onSubmit={(event) => this.handleOnSubmit(event)}>
<textarea rows={10} cols={30} value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
<br/>
<input type="submit" value="Save"/>
</form>
</div>
}
}
ReactDOM.render(<Editor />, document.getElementById("content"));
The versions of libraries are
"babel-cli": "6.24.1",
"babel-preset-react": "6.24.1"
"React & ReactDOM v15.5.4"
add a comment |
up vote
0
down vote
up vote
0
down vote
As a newbie in React world, I came across a similar issues where I could not edit
the textarea and struggled
with binding. It's worth knowing about controlled
and uncontrolled
elements when it comes to react.
The value of the following uncontrolled textarea
cannot be changed because of value
<textarea type="text" value="some value"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following uncontrolled textarea
can be changed because of use of defaultValue
or no value attribute
<textarea type="text" defaultValue="sample"
onChange={(event) => this.handleOnChange(event)}></textarea>
<textarea type="text"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following controlled textarea
can be changed because of how
value is mapped to a state as well as the onChange
event listener
<textarea value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
Here is my solution using different syntax. I prefer the auto-bind
than manual binding however, if I were to not use {(event) => this.onXXXX(event)}
then that would cause the content of textarea
to be not editable OR the event.preventDefault()
does not work as expected. Still a lot to learn I suppose.
class Editor extends React.Component {
constructor(props) {
super(props)
this.state = {
textareaValue: ''
}
}
handleOnChange(event) {
this.setState({
textareaValue: event.target.value
})
}
handleOnSubmit(event) {
event.preventDefault();
this.setState({
textareaValue: this.state.textareaValue + ' [Saved on ' + (new Date()).toLocaleString() + ']'
})
}
render() {
return <div>
<form onSubmit={(event) => this.handleOnSubmit(event)}>
<textarea rows={10} cols={30} value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
<br/>
<input type="submit" value="Save"/>
</form>
</div>
}
}
ReactDOM.render(<Editor />, document.getElementById("content"));
The versions of libraries are
"babel-cli": "6.24.1",
"babel-preset-react": "6.24.1"
"React & ReactDOM v15.5.4"
As a newbie in React world, I came across a similar issues where I could not edit
the textarea and struggled
with binding. It's worth knowing about controlled
and uncontrolled
elements when it comes to react.
The value of the following uncontrolled textarea
cannot be changed because of value
<textarea type="text" value="some value"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following uncontrolled textarea
can be changed because of use of defaultValue
or no value attribute
<textarea type="text" defaultValue="sample"
onChange={(event) => this.handleOnChange(event)}></textarea>
<textarea type="text"
onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following controlled textarea
can be changed because of how
value is mapped to a state as well as the onChange
event listener
<textarea value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
Here is my solution using different syntax. I prefer the auto-bind
than manual binding however, if I were to not use {(event) => this.onXXXX(event)}
then that would cause the content of textarea
to be not editable OR the event.preventDefault()
does not work as expected. Still a lot to learn I suppose.
class Editor extends React.Component {
constructor(props) {
super(props)
this.state = {
textareaValue: ''
}
}
handleOnChange(event) {
this.setState({
textareaValue: event.target.value
})
}
handleOnSubmit(event) {
event.preventDefault();
this.setState({
textareaValue: this.state.textareaValue + ' [Saved on ' + (new Date()).toLocaleString() + ']'
})
}
render() {
return <div>
<form onSubmit={(event) => this.handleOnSubmit(event)}>
<textarea rows={10} cols={30} value={this.state.textareaValue}
onChange={(event) => this.handleOnChange(event)}></textarea>
<br/>
<input type="submit" value="Save"/>
</form>
</div>
}
}
ReactDOM.render(<Editor />, document.getElementById("content"));
The versions of libraries are
"babel-cli": "6.24.1",
"babel-preset-react": "6.24.1"
"React & ReactDOM v15.5.4"
answered Nov 17 at 19:44
Raf
4,36611735
4,36611735
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33245017%2freact-modifying-textarea-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you looking for onChange, from the React docs? facebook.github.io/react/docs/forms.html#controlled-components
– Hypaethral
Oct 20 '15 at 19:22
On change is for when the value of the textarea changes. I don't really need that just want the textarea to be updated upon a state change of its parent class.
– phlie
Oct 20 '15 at 19:27
Are you sure the state of the parent is actually changing?
– Hypaethral
Oct 20 '15 at 19:28
I used a alert(this.state.fileData) to test and it seems to be. I also tried <textarea>{this.props.name}</textarea> but that doesn't work. If I change the value property it changes but then is uneditable.
– phlie
Oct 20 '15 at 19:31