site: site code style optimization (#40088)

This commit is contained in:
AkaraChen 2023-01-08 13:26:35 +08:00 committed by GitHub
parent 6c21f53e35
commit c0ce8e5455
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,33 +1,30 @@
import { JSONEditor as Editor, Mode, type JSONEditorPropsOptional } from 'vanilla-jsoneditor';
import React from 'react';
import React, { useEffect, useRef } from 'react';
import { JSONEditor, Mode } from 'vanilla-jsoneditor';
import type { JSONEditorPropsOptional } from 'vanilla-jsoneditor';
const JSONEditor = (props: JSONEditorPropsOptional) => {
const refContainer = React.useRef(null);
const refEditor = React.useRef(null);
const Editor: React.FC<JSONEditorPropsOptional> = (props) => {
const editorRef = useRef<JSONEditor>(null);
const container = useRef<HTMLDivElement>(null);
React.useEffect(() => {
refEditor.current = new Editor({
target: refContainer.current,
props: {
mode: Mode.text,
},
useEffect(() => {
editorRef.current = new JSONEditor({
target: container.current,
props: { mode: Mode.text },
});
return () => {
if (refEditor.current) {
refEditor.current.destroy();
refEditor.current = null;
if (editorRef.current) {
editorRef.current.destroy();
}
};
}, []);
React.useEffect(() => {
if (refEditor.current) {
refEditor.current.updateProps(props);
useEffect(() => {
if (editorRef.current) {
editorRef.current.updateProps(props);
}
}, [props]);
return <div className="vanilla-jsoneditor-react" ref={refContainer} />;
return <div ref={container} className="vanilla-jsoneditor-react" />;
};
export default JSONEditor;
export default Editor;