【发布时间】: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 中输入的任何内容,但在此组件本身的各个部分之间传递此值时遇到问题。谢谢。
【问题讨论】:
标签: reactjs typescript material-ui ace-editor