question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSONEditor can now be used as a controlled component

See original GitHub issue

@vankop I think you will like the latest release of the jsoneditor, v5.20.1, which allows using it as a controlled component in a framework like React. There are two new methods update(json) and updateText for this, and two new callback methods onChangeText and onChangeJSON (checkout the docs for details).

You can find two react examples in the examples folder for inspiration:

https://github.com/josdejong/jsoneditor/tree/master/examples/react_demo https://github.com/josdejong/jsoneditor/tree/master/examples/react_advanced_demo

It would be great if you could update jsoneditor-react to utilize this new functionality, and I would love to hear your feedback on whether it all works smooth or if you encounter issues.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

18reactions
Jezternzcommented, Mar 18, 2021

For anyone else who does not have time to take a new repo under their belt, this was my work-around to get controlled mostly working with the current version:

import React, { useEffect, useRef } from 'react';
import { JsonEditor } from 'jsoneditor-react';

import 'jsoneditor-react/es/editor.min.css';

export const ControlledJsonEditor = ({ value, onChange }) => {
    const jsonEditorRef = useRef();
    useEffect(
        () => {
            const editor = jsonEditorRef && jsonEditorRef.current && jsonEditorRef.current.jsonEditor;
            if(editor && value){ editor.update(value); }
        },
        [jsonEditorRef, value]
    )

    return (    
        <JsonEditor
            ref={jsonEditorRef}
            value={value}
            onChange={onChange}
        />
    );
}
1reaction
DominicTobiascommented, Apr 16, 2019

Awesome thanks @josdejong that component worked well, this one doesn’t update when the value changes

Read more comments on GitHub >

github_iconTop Results From Across the Web

jsoneditor-react-controlled - npm
Start using jsoneditor-react-controlled in your project by running `npm i ... You can use react-imported-component or your own implementation.
Read more >
Wrapping an external library as a controlled component with ...
I'm trying to make a thin wrapper to the "jsoneditor" library using a functionnal component. I'm rather new to React and worked so...
Read more >
JSON Editor - Contentstack
Using Contentstack Custom Fields, you can now input a JSON text into an entry field. With the JSON Editor custom field, you can...
Read more >
New Features in XMLSpy XML and JSON Editor - Altova
Once a schema is installed, it's available for use with any Altova ... XMLSpy users can now easily customize the display of the...
Read more >
Legacy Builder - Documentation - Prismic
Copy-paste the JSON code from the JSON editor and place it in the ... The type will now appear with the Active Custom...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found