tinymce / tinymce-react

Offical TinyMCE React component
MIT License
937 stars 152 forks source link

cursor to the first position when use onEditorChange function #444

Closed yuer0608 closed 7 months ago

yuer0608 commented 1 year ago

My code is similar that

function MyComponent({initialValue}) {
  const [value, setValue] = useState(initialValue ?? '');
  useEffect(() => setValue(initialValue ?? ''), [initialValue]);
  return (
    <Editor
      initialValue={initialValue}
      value={value}
      onEditorChange={(newValue, editor) => setValue(newValue)}
    />
  );
}

everytime, the cursor will to the first position when I edit content after. how I do ?

exalate-issue-sync[bot] commented 1 year ago

Ref: INT-3163

lorenzo-pomili commented 1 year ago

I cannot replicate it, can you provide use an example and the step by step instructions to replicate it?

also version of the editor and the browser are you using, could be a very useful information

duongnq2798 commented 1 year ago

This problem is likely due to the way the component updates its internal state.

The issue is related to how the TinyMCE editor's state is being managed in the parent component. Since the parent component is re-rendering, it's causing the cursor to jump back to the beginning of the content.

danoaky-tiny commented 7 months ago

I agree with @duongnq2798, closing.

hubertlepicki commented 6 months ago

I think this is indeed related to state management of the parent component but the thing that may be confusing is the naming of properties for the Editor, or how it works.

I found that changing initialValue={initialValue}, i.e. changing the variable intialValue results in precisely the above behavior, i.e. the cursor jumps to the beginning of the editor.

In my case the fix was not to update the initialValue

danoaky-tiny commented 6 months ago

@hubertlepicki Correct, you can see https://www.tiny.cloud/docs/tinymce/6/react-ref/#initialvalue for more info.