Skip to content

Instantly share code, notes, and snippets.

@searler
Created April 6, 2014 23:03
Show Gist options
  • Save searler/10012337 to your computer and use it in GitHub Desktop.
Save searler/10012337 to your computer and use it in GitHub Desktop.
react.js textarea that updates via a web service
var Editor = React.createClass({
getInitialState: function() {
return {value: 'Hello!'};
},
handleChange: function(event) {
var caretPos = this.getDOMNode().selectionStart;
var msg = {text:event.target.value,caret:caretPos};
$.ajax({url: "/text", type: "POST",
data: JSON.stringify(msg),
contentType:"application/json; charset=utf-8", dataType:"json",
success : function(response){
this.setState({value: response.text});
this.getDOMNode().setSelectionRange(response.caret,response.caret);
}.bind(this)});
},
render: function() {
var value = this.state.value;
return <textarea type="text" value={value}
onChange={this.handleChange}
rows="10" cols="60"/>;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment