React Modifying Textarea Values











up vote
12
down vote

favorite
1












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);









share|improve this question
























  • 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

















up vote
12
down vote

favorite
1












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);









share|improve this question
























  • 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















up vote
12
down vote

favorite
1









up vote
12
down vote

favorite
1






1





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);









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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






share|improve this answer























  • 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


















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"





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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






    share|improve this answer























    • 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















    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






    share|improve this answer























    • 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













    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






    share|improve this answer














    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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


















    • 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












    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"





    share|improve this answer

























      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"





      share|improve this answer























        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"





        share|improve this answer












        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"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 at 19:44









        Raf

        4,36611735




        4,36611735






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin