Reactjs 如何在单击按钮时从react ace编辑器获取新值
我试图在单击按钮时提醒ace编辑器的最终值。我知道Ace编辑器有一个onChange事件,只是不确定如何将该值输入我的handleClick函数 这是我当前的代码:Reactjs 如何在单击按钮时从react ace编辑器获取新值,reactjs,ace-editor,Reactjs,Ace Editor,我试图在单击按钮时提醒ace编辑器的最终值。我知道Ace编辑器有一个onChange事件,只是不确定如何将该值输入我的handleClick函数 这是我当前的代码: import ReactAce from "react-ace"; import React from "react"; import "ace-builds/src-noconflict/mode-python"; import "ace-builds/src
import ReactAce from "react-ace";
import React from "react";
import "ace-builds/src-noconflict/mode-python";
import "ace-builds/src-noconflict/theme-twilight";
function onChange(newValue) {
console.log("change", newValue);
}
function handleClick() {
alert();
}
function CodeEditor(props) {
return (
<>
<ReactAce
value={props.value}
mode="python"
theme="twilight"
showPrintMargin={false}
setReadOnly={false}
setValue={props.value}
fontSize={18}
style={{
height: "620px",
width: "100%",
}}
onChange={onChange}
/>
<button onClick={handleClick}>Run Code</button>
</>
);
}
export default CodeEditor;
从“react ace”导入ReactAce;
从“React”导入React;
导入“ace builds/src noconflict/modepython”;
导入“ace builds/src noconflict/theme twilight”;
函数onChange(newValue){
console.log(“更改”,newValue);
}
函数handleClick(){
警惕();
}
函数代码编辑器(道具){
返回(
运行代码
);
}
导出默认代码编辑器;
您可以使用useState钩子来管理文本的状态
function CodeEditor(props) {
// save the state here
const [ text, setText ] = useState('')
const handleClick = () => {
alert(text)
}
return (
<>
<ReactAce
value={props.value}
mode="python"
theme="twilight"
showPrintMargin={false}
setReadOnly={false}
setValue={props.value}
fontSize={18}
style={{
height: "620px",
width: "100%",
}}
// set the state when the value changes
onChange={(e) => setText(e.target.value)}
/>
<button onClick={() => handleClick()}>Run Code</button>
</>
函数代码编辑器(道具){
//在这里拯救国家
const[text,setText]=useState(“”)
常量handleClick=()=>{
警报(文本)
}
返回(
setText(e.target.value)}
/>
handleClick()}>运行代码
谢谢,尝试了它,但似乎得到了一个类型错误:无法读取未定义的属性'value'。这行似乎发生了:onChange={(e)=>setText(e.target.value)}。您需要传入的值,而不是e.target.value
。替换为onChange={value=>setText(value)}