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.

On OnChange the editor loses focus

See original GitHub issue

Hi, I need to use setContent on the event OnChange instead of on OnBlur but if I do this the editor loses the focus every time the function is called and some errors appear on the console. Does anyone know how to solve this? Or is it mandatory to use onBlur? Thanks!

import React, { useState, useRef } from "react";
import JoditEditor from "jodit-react";

const Editor = ({ value, onChange }: IEditorProps): JSX.Element => {

  const editor = useRef(null);
  const [content, setContent] = useState(value);

  const config = {
    readonly: false,
  };

  const handleChange = (newContent: string) => {
    setContent(newContent);
  };

  return (
        <JoditEditor ref={editor} value={content} config={config} onChange={handleChange} />
  );
};

interface IEditorProps {
  value: string;
  onChange: (value: string) => void;
}

export default Editor;
Captura de pantalla 2020-01-20 a las 13 12 59

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:17
  • Comments:48

github_iconTop GitHub Comments

16reactions
Sreejithinfocommented, Jul 12, 2020

The problem is onChange always re-render the state. So when re-render the focus will be lost. I have a solution to avoid this problem. Use useMemo hooks to solve this.

import React, { useRef, useState, useMemo } from 'react'
import JoditEditor from 'jodit-react'

const Editor = () => {
  const editor = useRef(null)
  const [content, setContent] = useState('')
  const config = {}
								
  return useMemo( () => ( 
                          <JoditEditor ref={editor} value={content} config={config} onChange={content => setContent(content)} /> 
                        ), [] )
}

export default Editor

13reactions
NKBelousovcommented, Jan 30, 2020

For some reason memoizing config helped me

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jodit react editor loses focus on onchange method
In react-jodit plugin, when we use onchange event, it loses focus from input after typing one character.
Read more >
React Text Input Losing Focus After Each Keypress
This bug took me a while to figure out, so I thought I would share the cause and the fix. I had a...
Read more >
Focusing: focus/blur - The Modern JavaScript Tutorial
The focus event is called on focusing, and blur – when the element loses the focus. Let's use them for validation of an...
Read more >
DropDown or ComboBox loses focus on change when used ...
DropDown or ComboBox loses focus on change when used as a custom editor with reference data for inline Grid editing.
Read more >
React.js loses input focus on typing
Then react re-rendered the page and updated the input. But because the key was different between re-renders it throws away the old input...
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