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

110
Vistas
Use mobX totally functional componet

I just learned react and react-native for 3 months , and i highly familiar with functional component, from EVERYTHING, because from first step, i already write a "hello world" in function, and unitl now, i still stick with function, because, i read some blog, and with hooks, learn classes are optional, so i decided to focus on functional component. But when i start to learn MobX, i feel a little bit confuse and struggle, especially with store:

class TodoStore {
    todos: Todo[] = []

    constructor() {
        makeObservable(this, {
            todos: observable,
            unfinishedTodoCount: computed,
            addTodo: action
        })
    }

    get unfinishedTodoCount() {
        return this.todos.filter(todo => !todo.done).length
    }

    addTodo(todo: Todo) {
        this.todos.push(todo)
    }
}

A silly-old question, but how constructor() use in functional component? Is that state? But how to apply with mobX, how to convert above class to function or can we just use function for all mobX concept?

Please help, thank you a lot

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Functional programming without dynamic state is just a big function that return the same value for the same parameters. In React you do have state that is stored in React instance itself, every time that you use setState React saves that value for future renders. it's holding the state for you.

As for MobX, you have to create an instance of an observable object and provide access to it to your components, so they can change it/interact with it.

The best practice for this, is to create an object instance out of a class and provide that instance to your components via the context api. (Read about it, if you don't know how to use it)

You CAN create a simple JavaScript object and make it observable using makeObservable(obj), but again. the best practice is to create a class and in the constructor call makeObservable(this).

Here's an example for a simple class Store:

class AgeStore {
  @observable age: number = 12;

  constructor() {
    makeObservable(this);
  }

  @action
  grow() {
    this.age++;
  }
}

And this is how you would initialize your React app (You should split this to several files):


const ageStore = new AgeStore();
const AppContext = createContext(null);

export function useAgeStore() {
  return useContext(AppContext);
}

ReactDOM.render(
  <AppContext.Provider value={ageStore}>
    <App />,
  </AppContext.Provider>
  document.getElementById('app'),
);

And this is how your functional components should use the store:

export const DemoComp: React.FC = observer(function DemoComp() {
  const ageStore = useAgeStore();

  return <div>
    <div>The current Age is:{ageStore.age}</div>
    <button onClick={ageStore.grow()}>Grow</button>
  </div>;
});
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