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

174
Vistas
DraftJS handle focus when page has multiple editor (editable and readonly)

I'm facing issue with follow DraftJS editor component.

import React, { useRef } from "react";
import styled from "styled-components";
import { EditorState, Editor as DraftEdior } from "draft-js";

const Wrapper = styled.div`
  border: 1px solid #ccc;
  padding: 1rem;
`;

interface IEditorProps {
  state: EditorState;
  onChange: (state: EditorState) => void;
  readOnly?: boolean;
}

export const Editor: React.FunctionComponent<IEditorProps> = (props) => {
  const { state, onChange, readOnly } = props;

  const ref = useRef<DraftEdior | null>(null);

  return (
    <Wrapper onClick={() => ref?.current?.focus()}>
      <DraftEdior
        ref={ref}
        editorState={state}
        onChange={onChange}
        readOnly={!!readOnly}
      />
    </Wrapper>
  );
};

export default Editor;

and the App.js

import React from "react";
import { EditorState } from "draft-js";
import { Editor } from "./Editor";

const App = () => {
  const [state, setState] = React.useState<EditorState>(() =>
    EditorState.createEmpty()
  );
  return (
    <>
      <h3>Editor</h3>
      <Editor state={state} onChange={(state) => setState(state)} />

      <h3>Result</h3>
      <Editor state={state} readOnly onChange={() => null} />
    </>
  );
};

export default App;

When only a single editor on the page, everything work ok. But the editor continuously lost focus when I add a readonly component.

This is a code sanbox link

https://codesandbox.io/s/vigilant-wilson-70op8?file=/src/App.tsx:0-451

How to fix it, thank you very much.

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