【问题标题】:How to pass the value of AceEditor to the component state using the onClick of a button? ReactJS如何使用按钮的 onClick 将 AceEditor 的值传递给组件状态?反应JS
【发布时间】:2021-06-14 15:04:17
【问题描述】:

我目前正在尝试在 ACE 编辑器的帮助下实现某种 CodeMirror。我尝试在“onClick”按钮参数旁边使用状态,但实际上无法正常工作。

import React, { Component } from 'react';
import { makeStyles, createStyles } from '@material-ui/core/styles';
import './CodeMirror.css';
import { Box, Button } from '@material-ui/core';
// Import Brace and the AceEditor Component
import AceEditor from 'react-ace';
import TryItButton from '../CustomButton/TryItButton';
// Import a Mode (language)
import 'ace-builds/src-noconflict/mode-javascript';
// Import a Theme (okadia, github, xcode etc)
import 'ace-builds/src-noconflict/theme-twilight';

export interface AppProps {
  headings: {
    key: number;
    name: string;
    pathname: string;
  }[];
  subheadings: {
    key: number;
    name: string;
    calls: string[];
  }[];
}

interface AppState {
  value: string;
}

function onChange(newValue: string) {
  console.log('CodeMirror value: ', newValue);
}

export default class CodeMirror extends Component<AppProps, AppState> {
  constructor(props: AppProps) {
    super(props);
    this.state = { value: '' };
  }

  render(): JSX.Element {
    return (
      <>
        <div className="main-container-codemirror">
          <div className="top-block-codemirror">
            <Box className="top-bar-text-codemirror">
              <i className="white">Example Call</i>
            </Box>
          </div>

          <div className="example-block-codemirror">
            <div className="textarea-example">
              <AceEditor
                // placeholder="Enter a call here..."
                mode="javascript"
                theme="twilight"
                name="ace-editor"
                fontSize={12}
                showPrintMargin
                wrapEnabled
                showGutter
                highlightActiveLine
                value={this.state.value}
                setOptions={{
                  enableBasicAutocompletion: true,
                  enableLiveAutocompletion: true,
                  enableSnippets: false,
                  showLineNumbers: true,
                  tabSize: 2,
                  useWorker: false,
                }}
                style={{
                  position: 'relative',
                  width: '100%',
                  height: '100%',
                }}
                onChange={onChange}
              />
            </div>

            <div className="spacer">
              <Button
                className="try-it-button"
                style={{
                  backgroundColor: '#533cf8',
                  color: 'white',
                  borderRadius: 0,
                  fontSize: 13,
                  fontWeight: 200,
                }}
              >
                Try it!
              </Button>
              <div className="spacer-text-div">
                auto-update 'fat', alphabetize payload, and make the example
                call below
              </div>
            </div>

            <div className="header-2">
              <i className="white">Example Response</i>
            </div>

            <div className="textarea-example">
              <textarea
                readOnly
                className="example-code"
                value="Response code"
              />
            </div>

            <div className="bottom-block-codemirror" />
          </div>
        </div>
      </>
    );
  }
}

到目前为止,这是我的代码,但我要做的是让输入到 AceEditor 组件中的文本存储在组件状态中,在选择“尝试”后(见下文。它将是很高兴将输入的文本也显示在底部文本区域中,但是在状态下获得这个值足以让我继续这个项目的其余部分。

我目前正在控制台中使用“onChange”显示在 AceEditor 中输入的任何内容,但在此组件本身的各个部分之间传递此值时遇到问题。谢谢。

CodeMirror Component

【问题讨论】:

    标签: reactjs typescript material-ui ace-editor


    【解决方案1】:

    试试这个方法,

    import "./styles.css";
    
    import React, { Component } from "react";
    import { makeStyles, createStyles } from "@material-ui/core/styles";
    // import './CodeMirror.css';
    import { Box, Button } from "@material-ui/core";
    // Import Brace and the AceEditor Component
    import AceEditor from "react-ace";
    import TryItButton from "../CustomButton/TryItButton";
    // Import a Mode (language)
    import "ace-builds/src-noconflict/mode-javascript";
    // Import a Theme (okadia, github, xcode etc)
    import "ace-builds/src-noconflict/theme-twilight";
    
    export default function App() {
      return (
        <div className="App">
          <h1>Hello CodeSandbox</h1>
          <CodeMirror />
          dsdsd
        </div>
      );
    }
    
    export interface AppProps {
      headings?: {
        key: number;
        name: string;
        pathname: string;
      }[];
      subheadings?: {
        key: number;
        name: string;
        calls: string[];
      }[];
    }
    
    interface AppState {
      value: string;
      textAreaValue?: string;
    }
    
    class CodeMirror extends Component<AppProps, AppState> {
      constructor(props: AppProps) {
        super(props);
        this.state = { value: "", textAreaValue: "" };
      }
    
      onChange(newValue: string) {
        console.log("CodeMirror value: ", newValue);
        this.setState({ value: newValue });
      }
    
      render() {
        return (
          <>
            <div className="main-container-codemirror">
              <div className="top-block-codemirror">
                <Box className="top-bar-text-codemirror">
                  <i className="white">Example Call</i>
                </Box>
              </div>
    
              <div className="example-block-codemirror">
                <div className="textarea-example">
                  <AceEditor
                    // placeholder="Enter a call here..."
                    mode="javascript"
                    theme="twilight"
                    name="ace-editor"
                    fontSize={12}
                    showPrintMargin
                    wrapEnabled
                    showGutter
                    highlightActiveLine
                    value={this.state.value}
                    setOptions={{
                      enableBasicAutocompletion: true,
                      enableLiveAutocompletion: true,
                      enableSnippets: false,
                      showLineNumbers: true,
                      tabSize: 2,
                      useWorker: false
                    }}
                    style={{
                      // position: 'relative',
                      width: "100%",
                      height: "200px"
                    }}
                    onChange={(e) => this.onChange(e)}
                  />
                </div>
    
                <div className="spacer">
                  <Button
                    className="try-it-button"
                    onClick={() =>
                      this.setState((prev) => ({
                        ...prev,
                        textAreaValue: prev["value"]
                      }))
                    }
                    style={{
                      backgroundColor: "#533cf8",
                      color: "white",
                      borderRadius: 0,
                      fontSize: 13,
                      fontWeight: 200
                    }}
                  >
                    Try it!
                  </Button>
                  <div className="spacer-text-div">
                    auto-update 'fat', alphabetize payload, and make the example
                    call below
                  </div>
                </div>
    
                <div className="header-2">
                  <i className="white">Example Response</i>
                </div>
    
                <div className="textarea-example">
                  <textarea
                    readOnly
                    className="example-code"
                    value={this.state.textAreaValue || "Response code"}
                  />
                </div>
    
                <div className="bottom-block-codemirror" />
              </div>
            </div>
          </>
        );
      }
    }
    

    codeSandbox - https://codesandbox.io/s/reverent-microservice-yp2ff?file=/src/App.tsx:0-3764

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 2021-10-23
      • 1970-01-01
      相关资源
      最近更新 更多