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

91
Vistas
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 Respuestas
Responde la pregunta

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 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