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

93
Visualizações
use of conventional Javascript classes for models in React

Is it possible to use conventional Javascript classes in models (in a MVC paradigm) using React instead of using Redux or contexts & reducers that seem to undermine reusability? If so, how can we effectively 'subscribe' to actions that change data in these objects so that views (i.e. React components) are rendered effectively?

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

0

The subscription logic is on you to figure out yourself. React doesn't offer any mechanism that can be "borrowed" in other classes. But event-oriented programming in Javascript is, of course, nothing new. So an event-emitter like pattern would work. So here is one example (not tested, but it should give you the idea):

class MyClass {

  data = {};

  constructor(onTick) {
    setInterval(() => {
      const now = new Date();
      this.data.time = now;
      this.data.tillChristmas = (new Date('2021-12-25')) - now;
      onTick && onTick(this);
    }, 10 * 1e3);
  }
}

const MyComp = () => {

  const [data, setData] = useState({});
  useEffect(() => {
      new MyClass(myclass => {
        setData(myclass.data);
      });
    }, []);

  return <pre>
    {JSON.stringify(data, true, 2)}
  </pre>;
  // of course, you could present the data in more interesting ways, too.
};
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