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

Editor loses focus when updating state #134

Open
olaven opened this issue Jun 19, 2020 · 6 comments
Open

Editor loses focus when updating state #134

olaven opened this issue Jun 19, 2020 · 6 comments

Comments

@olaven
Copy link

olaven commented Jun 19, 2020

Hi! We are looking into using this package for an application and it looks promising! Thanks!
This may be due to an understanding, but we are having some issues.

Given the example below, these are the observations:

  • setting the state manually causes editor focus to be lost (i.e. have to click on editor again)
  • if manual state update is removed, it seems like the editor updates it automatically

In other words, the correct approach seems to be to just pass value and never handle the state.
This is totally fine 👍 Great even, but not obvious (to me, at least).

However, loosing focus is still weird, as appears to be kept in the component.
Is this behaviour intended?

import React, { useState, useRef, useEffect } from 'react';
import dynamic from 'next/dynamic';
import MarkdownIt from 'markdown-it'
import 'react-markdown-editor-lite/lib/index.css';


export const SimpleMDEditor = () => {

    const [md, setMD] = useState("")

    const mdParser = new MarkdownIt();
    const MdEditor = dynamic(() => import('react-markdown-editor-lite'), {
        ssr: false
    });

    const handleEditorChange = ({ html, text }) => {

        console.log("text", text);
        //REMOVE LINE BELOW -> Focus is not lost 
        setMD(text);
    }

    return <MdEditor
        // key="markdownEditor"
        id="test"
        value={md}
        style={{ height: "500px" }}
        renderHTML={(text) => mdParser.render(text)}
        onChange={handleEditorChange}
    />
}
@sylingd
Copy link
Collaborator

sylingd commented Jun 21, 2020

What system and browser you are using?

@olaven
Copy link
Author

olaven commented Jun 21, 2020

@sylingd Chrome/Firefox and the latest MacOS :-)

@erlendoeien
Copy link

@olaven I'm struggling with a similar issue, but not with this package.
If the Editor is nested in another components, theres probably an issue with the rerendering.
This and this might give some insight (I've not solved the issue myself yet).

If you figured it out, I would love some feedback/pointers! 😃

@olaven
Copy link
Author

olaven commented Aug 11, 2020

Hi, @erlendoeien! Sorry for the late response.
The solution was related to rerendering! I'm not entirely sure, but I think we ended up keeping the
markdown-state in a parent. This was a collaboration effort and it's been a while, so I don't remember the details..

@masaez
Copy link

masaez commented May 3, 2021

If anyone bumbs with this issue, I was able to fix it by NOT using useState. I am currently declaring a variable outside the render function, and just updating that function. I am using Next.js as reference.

@jaddoescad
Copy link

@masaez hey masaez, could you share a code snippet of how you were able to solve this issue?

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

No branches or pull requests

5 participants