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

150
Visualizações
Update input value from mobx store with debounce

I have a MobX store and 2 Components, connected to the same value:

import { makeAutoObservable } from 'mobx';
import { observer } from 'mobx-react-lite';

const store = makeAutoObservable({
  value: 0,
  setValue: (v) => {
    this.value = v;
  },
});

const ComponentOne = observer(() => {
  function handleOnChange(e) {
    store.setValue(e.target.value);
  }

  return <input value={store.value} onChange={handleOnChange} />;
});

const ComponentTwo = observer(() => {
  function handleOnChange(e) {
    store.setValue(e.target.value);
  }

  return <input value={store.value} onChange={handleOnChange} />;
});

I need:

  1. Changing the input value of <ComponentOne/> should cause the input value in <ComponentTwo/> to be updated from the new store value
  2. Storing data into MobX storage from Components should be done with debounce.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

My Solution:

import { makeAutoObservable } from 'mobx';
import { observer } from 'mobx-react-lite';
import { useDebouncedCallback } from 'use-debounce';

const store = makeAutoObservable({
  value: 0,
  setValue: (v) => {
    this.value = v;
  },
});

export function useStoreValue() {
  const storeValue = store.value;
  const setStoreValue = store.setValue;
  const [value, setValue] = useState(storeValue);

  // eslint-disable-next-line object-curly-newline
  const debouncedSetStoreValue = useDebouncedCallback(setStoreValue, 350, { maxWait: 1000 });

  useEffect(
    () => {
      if (storeValue !== value) {
        debouncedSetStoreValue(value);
      }
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [value]
  );

  useEffect(() => {
    setValue(storeValue);
  }, [storeValue]);

  return [value, setValue];
}

const ComponentOne = observer(() => {
  const [value, setValue] = useStoreValue();

  function handleOnChange(e) {
    setValue(e.target.value);
  }

  return <input value={value} onChange={handleOnChange} />;
});

const ComponentTwo = observer(() => {
  const [value, setValue] = useStoreValue();

  function handleOnChange(e) {
    setValue(e.target.value);
  }

  return <input value={value} onChange={handleOnChange} />;
});
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