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

Using onChange to store value in state results in jumping keyboard cursor #167

Open
antun opened this issue Jul 17, 2023 · 0 comments
Open

Comments

@antun
Copy link

antun commented Jul 17, 2023

I'm not sure if this is a bug or I'm not following the correct pattern. I need to store the code that the user edited in the state. (In my app, I'm using Redux, but I've simplified it to just React state with useState for this example.)

import { useState } from 'react'
import {
  LiveProvider,
  LiveEditor,
  LiveError,
  LivePreview,
} from 'react-live-runner'
import './App.css'

function App() {
  const [count, setCount] = useState(0)
  const initCode = `<div>
  Hello, world!
</div>`;
  const [code, setCode] = useState(initCode);
  const handleChange = (newCode) => {
    setCode(newCode);
  };

  return (
    <>
      <LiveProvider code={code}>
        <LiveEditor onChange={handleChange} />
        <LivePreview />
        <LiveError />
      </LiveProvider>
      <hr />
      <div>code:</div>
      <pre>
        {code}
      </pre>
    </>
  )
}

export default App              

When I type into the LiveEditor, the first character is inserted into the correct place. However, after that,the cursor jumps to the end of the LiveEditor, and any following characters are inserted there.

I figured saving the code in state is probably fairly common, so maybe I'm just not following the right pattern here?

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

1 participant