admin管理员组文章数量:1289638
I am facing the following issue while using AceEditor
react ponent
I am using AceEditor
as user input, after user enters code, he(she) presses the Run
button. (see the picture) How do I extract the text that users enters from AceEditor
ponent ?
I am facing the following issue while using AceEditor
react ponent https://github./securingsincity/react-ace
I am using AceEditor
as user input, after user enters code, he(she) presses the Run
button. (see the picture) How do I extract the text that users enters from AceEditor
ponent ?
5 Answers
Reset to default 4It's not necessary to use onChange.
<AceEditor ref="aceEditor" />
this.refs.aceEditor.editor.getValue()
You need to subscribe to the onChange
event (explained in the docs) and store the value passed into the callback somewhere, perhaps in the ponent's state if the Run
button is on the same page. Then, when user clicks the button just retrieve it via this.state.xxx
With the latest React v16.12+ this.refName.current.editor.getValue()
works to get string value which can be parsed using JSON.parse
.
Ref should be instantiated as:
constructor(props) {
super(props);
this.refName = React.createRef();
}
and passed to AceEditor ponent:
<AceEditor
ref={this.refName}
/>
AceEditor provides an onChange
event which you can use to retrieve the current content of the editor whenever the user changes it and then store the value in your own data store or your ponent's state.
This way, you are able to retrieve the value whenever you need it.
More about the editor's properties.
The readme also provides an example, demostrating its usage.
You need to bind this state to the onchange function in constructor of class.It worked for me.
constructor(props){
super(props);
this.state = {code:"code"};
this.onChange = this.onChange.bind(this);
}
onChange(newValue) {
this.state.code = newValue;
alert(this.state.code);
}
Ace Editor's Onchange is
onChange = {
this.onChange
}
本文标签: javascriptHow to get the value of react componentAceEditorStack Overflow
版权声明:本文标题:javascript - How to get the value of react component - AceEditor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741412299a2377308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论