Get reference of instance in functional component React

Viewed 771

I am trying to add a template onClick, I am using ckeditor library and I want to get the instance of editor so that I can pass it and use the methods of it to the other components. But I've no idea how to do that, been searching it for a while but I cant put the right words on it. I am using functional component.

let ckeditor;

      <CKEditorContext context={Context}>
        <CKEditor
          editor={ClassicEditor}
          data={props.state.wysiwygData}
          onChange={props.editorOnChangeHandler}
          ref={props.editorRef}
          onReady={(editor) => {
            // I want to Pass the 'editor' instance to ckReference variable
              ckReference = editor;
            // But when I do this kind of approach it becomes undefined when passed
          }}
        />
      </CKEditorContext>

Thank you in advance and God bless

1 Answers

So I've finally got the editor instance using useRef() hook like this and now can be passed on to the other components.

let ckeditor = useRef(null);

     <CKEditorContext context={Context}>
        <CKEditor
          editor={ClassicEditor}
          data={props.state.wysiwygData}
          onChange={props.editorOnChangeHandler}
          ref={props.editorRef}
          onReady={(editor) => {
            ckeditor.current = editor;
          }}
        />
      </CKEditorContext>
Related