Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapping in React Component #4

Closed
on2air opened this issue Mar 16, 2021 · 5 comments
Closed

Wrapping in React Component #4

on2air opened this issue Mar 16, 2021 · 5 comments
Labels
question Further information is requested

Comments

@on2air
Copy link

on2air commented Mar 16, 2021

Attempting to wrap in a React component. I'm able to get it to display, although not perfectly. But then after a couple seconds it generates an error:
TypeError: Cannot read property 'ownerDocument' of undefined

Here is the code in the wrapper:

import React, {useRef} from 'react'
import { JSONEditor } from 'svelte-jsoneditor/dist/jsoneditor.js'

const JSONEditorComponent = ( {} ) => {
    const editor = useRef(null)

    const onRef = ( ref ) => {
        if(ref) {
            console.log('ON REF', ref)
            editor.current = new JSONEditor({
                target: ref,
                props: {
                    json: {
                        greeting: 'Hello World'
                    },
                    onChange: (content) => {
                        // content is an object { json: JSON | undefined, text: string | undefined }
                        console.log('onChange', content)

                    }
                }
            })
        }
    }

    return (
        <div id="json-editor" ref={onRef}/>
    )
}

Any one more advanced with React able to get this working?

@josdejong
Copy link
Owner

I created a sandbox based on your code example, it all seems to work just fine:

https://codesandbox.io/s/svelte-jsoneditor-react-59wxz?file=/src/App.js

@josdejong josdejong added the question Further information is requested label Mar 20, 2021
@on2air
Copy link
Author

on2air commented Mar 20, 2021

thank you @josdejong - your slight changes made a difference.
I've modified it to add the destroy on unmount fix for anyone looking in the future:

https://codesandbox.io/s/svelte-jsoneditor-react-forked-p48uq

@josdejong
Copy link
Owner

Ah, ok good to hear it works for you now! Thanks for sharing your updated sandbox.

@lsbyerley
Copy link

Is creating a ref necessary in the posted codesandbox examples when implementing this in react?

@josdejong
Copy link
Owner

Yes. Try to do it without a ref it and you'll find out very soon :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants