Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

315
Vistas
React: change contentEditable div's state and maintain cursor

I am developing a writing app for my admin panel, and it has a multilingual state... so each input box looks like this:

<input
  type='text'
  name='title'
  placeholder='title'
  onInput={
    e => setPostState({
      ...postState,
      [lang]: {
        ...postState[lang],
        title: e.target.value
      }
    }) }
  value={ postState[lang].title }
/>

It works perfectly, I can type whatever, and if I switch languages it switches value, the value syncs with the state and there's no problem with the cursor... until I get to a custom contentEditable div input (which acts as an HTML editor, complete with syntax highlighting):

<Editor
  editorRef={ Ref.editor }
  onInput={ txt => ... }
  value=...
/>

My editor handles input with this function:

const handleInput = e =>
{
  const { current: editor } = editorRef;
  onInput(editor.textContent);
  doHighlight(editor);
  e.preventDefault();
}

and updates its textContent based on its value here:

useEffect(() =>
{
  if (value) editorRef.current.textContent = value;
}, [ value ]);

Problem is, is that the state reload from value triggers the component to render... which resets the cursor. Why is this not a problem on a normal input component, and how can I emulate this behavior in a div?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda