Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

315
Visualizações
How can I debounce editor value when the value is managed by parent component?

Editor component

import Editor from '@monaco-editor/react';
import { useDebounce } from './useDebounce';
import { useEffect, useState } from 'react';

type Props = {
  code: string;
  onChange: (code: string) => void;
  disabled?: boolean;
};

export const GraphqlCodeEditor = ({
  onChange,
  code,
  disabled = false,
}: Props) => {
  const [editorValue, setEditorValue] = useState(code);
  const editorValueDebounced = useDebounce(editorValue, 500);

  useEffect(() => {
    onChange(editorValueDebounced);
  }, [editorValueDebounced, onChange]);

  useEffect(() => {
    if (code !== editorValueDebounced) {
      setEditorValue(code);
    }
  }, [code, editorValueDebounced]);

  return (
    <Editor
      options={{
        minimap: { enabled: false },
        autoClosingBrackets: 'always',
        readOnly: disabled,
      }}
      language="graphql"
      value={editorValue}
      onChange={(value) => {
        if (value) {
          setEditorValue(value);
        }
      }}
    />
  );
};

useDebounce hook

import { useEffect, useState } from 'react';

export const useDebounce = <T>(value: T, delay: number) => {
  // State and setters for debounced value
  const [debouncedValue, setDebouncedValue] = useState(value);
  useEffect(
    () => {
      // Update debounced value after delay
      const handler = setTimeout(() => {
        setDebouncedValue(value);
      }, delay);
      // Cancel the timeout if value changes (also on delay change or unmount)
      // This is how we prevent debounced value from updating if value is changed ...
      // .. within the delay period. Timeout gets cleared and restarted.
      return () => {
        clearTimeout(handler);
      };
    },
    [value, delay] // Only re-call effect if value or delay changes
  );
  return debouncedValue;
};

The code prop in the editor component is managed by a parent component. When e.g a user loads a new code snippet it's updated and the editor should load the new value. The editor should also debounce it's value so the onChange() function isn't called on every keypress.

The editor component above results in a loop where the component switches between the previous value and the new every 500ms.

How can I achieve this with the useDebounce hook?

This is closely related to this post I think, however it's using lodash debounce. I was hoping to achieve the same with the useDebounce hook.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem was this useEffect()

useEffect(() => {
  if (code !== editorValueDebounced) {
   setEditorValue(code);
  }
}, [code, editorValueDebounced]);

Changing it to this fixed the problem

useEffect(() => {
  setEditorValue(code);
}, [code]);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda