Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!