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

264
Vistas
How to force new state in React (Hooks) for textarea when clicking on another element?
  • codesandbox.io sandbox
  • github.com repository

I am creating this small Wiki project.

My main component is Editor(), which has handleClick() and handleModeChange() functions defined.

handleClick() fires when a page in the left sidebar is clicked/changed.

handleModeChange() switches between read and write mode (the two icon buttons in the left sidebar).

When in read mode, the clicking on different pages in the left sidebar works properly and changes the main content on the right side.

However, in write mode, when the content is echoed inside <TextareaAutosize>, the content is not changed when clicking in the left menu.

In Content.js, I have:

<TextareaAutosize
  name="textarea"
  value={textareaValue}
  minRows={3}
  onChange={handleMarkdownChange}
/>

textareaValue is defined in Content.js as:

const [textareaValue, setTextareaValue] = useState(props.currentMarkdown);

const handleMarkdownChange = e => {
  setTextareaValue(e.target.value);
  props.handleMarkdownChange(e.target.value);
};

I am unsure what is the correct way to handle this change inside textarea. Should I somehow force Editor's child, Content, to re-render with handleClick(), or am I doing something completely wrong and would this issue be resolved if I just changed the definition of some variable?

I have been at this for a while now...

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You just need to update textareaValue whenever props.currentMarkdown changes. This can be done using useEffect.

useEffect(() => {
  setTextareaValue(props.currentMarkdown);
}, [props.currentMarkdown]);

Edit reverent-breeze-cwyfw


Problem:

const [textareaValue, setTextareaValue] = useState(props.currentMarkdown);

useState will use props.currentMarkdown as the initial value but it doesn't update the current state when props.currentMarkdown changes.


Unrelated:

The debounce update of the parent state can be improved by using useRef

const timeout = useRef();

const handleMarkdownChange = (newValue) => {
    if (timeout.current) {
        clearTimeout(timeout.current);
    }
    timeout.current = setTimeout(function () {
        console.log("fire");
        const items = [];
        for (let value of currentData.items) {
            if (value["id"] === currentData.active) {
                value.markdown = newValue;
                value.unsaved = true;
            }
            items.push(value);
        }
        setCurrentData({ ...currentData, items: items });
    }, 200);
};

using var timeout is bad because it declares timeout on every render, whereas useRef gives us a mutable reference that is persisted across renders

about 4 years ago · Santiago Trujillo Denunciar
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