Codemirror在单行上显示JSON

问题描述 投票:0回答:1

使用react-codemirror2和react-jsonschema-form开始一个新项目非常相似https://mozilla-services.github.io/react-jsonschema-form/

但是,当我的codemirror编辑器呈现JSON时,我在一行上加载所有节目。我已经浏览了https://mozilla-services.github.io/react-jsonschema-form/的源代码,找不到与我有什么不同的东西。

enter image description here

我的源代码:

    import React, { useEffect, useState } from "react";
import { UnControlled as CodeMirror } from "react-codemirror2";

import "codemirror/lib/codemirror.css";
import "codemirror/theme/material.css";
import "codemirror/mode/javascript/javascript.js";

// components

const CodeEditorContainer = ({ code, onChange }) => {
  const [codeEditorState, setCodeEditorState] = useState();

  useEffect(() => {
    setCodeEditorState(code);
  }, [code]);

  const cmOptions = {
    theme: "default",
    height: "auto",
    viewportMargin: Infinity,
    mode: {
      name: "javascript",
      json: true,
      statementIndent: 2
    },
    lineNumbers: true,
    lineWrapping: true,
    indentWithTabs: false,
    tabSize: 2
  };

  return (
    <div className="panel panel-default">
      <div className="panel-heading">Schema Editor</div>
      <CodeMirror
        value={codeEditorState}
        options={cmOptions}
        autoCursor={false}
        onChange={(editor, data, value) => onChange(value)}
      />
    </div>
  );
};

export default CodeEditorContainer;

编辑:问题是我将JSON解析为字符串而不是

JSON.stringify(json)

我用了

JSON.stringify(json, null, 2)

reactjs codemirror react-codemirror
1个回答
0
投票

问题是我将JSON解析为字符串而不是

JSON.stringify(json)

我用了

JSON.stringify(json, null, 2)

© www.soinside.com 2019 - 2024. All rights reserved.