簡體   English   中英

如何在單擊按鈕時從 react ace 編輯器中獲取新值

[英]How to get new value from react ace editor on button click

我試圖在單擊按鈕時提醒 ace 編輯器的最終值。 我知道 Ace Editor 有一個 onChange 事件,只是不確定如何將該值放入我的handleClick函數中。

這是我當前的代碼:

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;

您可以使用 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>
    </>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM