jsoneditor-react icon indicating copy to clipboard operation
jsoneditor-react copied to clipboard

update peer dependency to React 18

Open Hussein-Shaito opened this issue 2 years ago • 6 comments
trafficstars

can please update the dependency to be compatible to the lastest version of react (18). Otherwise npm i fails with latest npm.

Hussein-Shaito avatar Jun 19 '23 10:06 Hussein-Shaito

I had the same problem with React 18 and found this fix:

npm i jsoneditor
npm i --save @types/jsoneditor

Next, here's the JSONEditorComponent:

import React, { useEffect, useRef } from "react";
import JSONEditor, { JSONEditorOptions } from "jsoneditor";
import "jsoneditor/dist/jsoneditor.css";
import { Card } from "primereact/card";

interface JSONEditorComponentProps {
  jsonData: any;
}

const JSONEditorComponent: React.FC<JSONEditorComponentProps> = ({ jsonData }) => {
  const containerRef = useRef<HTMLDivElement | null>(null);
  let jsonEditor: JSONEditor | null = null;

  useEffect(() => {
    if (containerRef.current) {
      const options: JSONEditorOptions = {};
      jsonEditor = new JSONEditor(containerRef.current, options);
      jsonEditor.set(jsonData);
    }

    return () => {
      if (jsonEditor) {
        jsonEditor.destroy();
      }
    };
  }, [jsonData]);

  return (
    <Card>
      <div ref={containerRef} style={{width: '100%', height: '400px'}}/>
    </Card>
  );
};

export default JSONEditorComponent;

In this TypeScript version, we have defined an interface JSONEditorComponentProps for the props that the JSONEditorComponent accepts, and we've used React.FC<JSONEditorComponentProps> to define the type of the component. Also, jsonEditor and containerRef variables are defined with their specific types.

Here is how you can use the JSONEditorComponent:

import React from "react";
import JSONEditorComponent from "./JSONEditorComponent";

const App: React.FC = () => {
  const data = {
    "name": "John",
    "age": 30,
    "city": "New York"
  };

  return <JSONEditorComponent jsonData={data} />;
};

export default App;

mattqs avatar Jul 06 '23 07:07 mattqs

Came to request the same thing. This is a great package - thanks to the devs working on it!

bkiggen avatar Aug 30 '23 15:08 bkiggen

I had the same problem with React 18 and found this fix:

npm i jsoneditor
npm i --save @types/jsoneditor

Next, here's the JSONEditorComponent:

import React, { useEffect, useRef } from "react";
import JSONEditor, { JSONEditorOptions } from "jsoneditor";
import "jsoneditor/dist/jsoneditor.css";
import { Card } from "primereact/card";

interface JSONEditorComponentProps {
  jsonData: any;
}

const JSONEditorComponent: React.FC<JSONEditorComponentProps> = ({ jsonData }) => {
  const containerRef = useRef<HTMLDivElement | null>(null);
  let jsonEditor: JSONEditor | null = null;

  useEffect(() => {
    if (containerRef.current) {
      const options: JSONEditorOptions = {};
      jsonEditor = new JSONEditor(containerRef.current, options);
      jsonEditor.set(jsonData);
    }

    return () => {
      if (jsonEditor) {
        jsonEditor.destroy();
      }
    };
  }, [jsonData]);

  return (
    <Card>
      <div ref={containerRef} style={{width: '100%', height: '400px'}}/>
    </Card>
  );
};

export default JSONEditorComponent;

In this TypeScript version, we have defined an interface JSONEditorComponentProps for the props that the JSONEditorComponent accepts, and we've used React.FC to define the type of the component. Also, jsonEditor and containerRef variables are defined with their specific types.

Here is how you can use the JSONEditorComponent:

import React from "react";
import JSONEditorComponent from "./JSONEditorComponent";

const App: React.FC = () => {
  const data = {
    "name": "John",
    "age": 30,
    "city": "New York"
  };

  return <JSONEditorComponent jsonData={data} />;
};

export default App;

this was very helpful thanks

MarisaCodes avatar Oct 14 '23 23:10 MarisaCodes

This almost feels like it should just be a new repo / npm package :D

Thanks for the sample code, working on a slightly modified version that enables you to pass in options, utilize onChange and value in a react controlled input fashion.

Jezternz avatar Jan 31 '24 22:01 Jezternz

Ended up just going with a custom js and no longer using jsoneditor-react, can see source here if interested: https://gist.github.com/Jezternz/fd2edd87f9b31c66eb9258d75afed600

Jezternz avatar Feb 03 '24 04:02 Jezternz