“Simplifying State Management in React Native with MobX: A Developer’s Guide to Seamless App Control”

Covenant Ifeoluwa
3 min readAug 28, 2023

--

State management is critical to developing robust and efficient applications, especially in the context of React Native, where maintaining a seamless user experience across different platforms is essential. While various state management libraries are available, MobX stands out for its simplicity and flexibility. In the previous article, we explored managing global state with React Context, now we’ll explore what MobX is, its core concepts, and how it can be used for state management in React Native applications.

What is MobX?

MobX is a state management library that helps you manage the state of your application in a more organized and reactive way. It focuses on making state management simple and intuitive by leveraging observable data structures and automatic updates. With MobX, you can declaratively define your application’s state, and the library efficiently updates the UI whenever the state changes.

Core Concepts of MobX

1) Observables

In MobX, observables are the foundation of reactive programming. An observable is a piece of data that, when changed, triggers reactions. These reactions could be updates to the UI or any other behavior you define. In the context of state management, your application’s state variables become observable.

2) Actions

Actions in MobX are functions that modify the observables. They are the only way to modify the state. By using actions, MobX ensures that state changes are tracked and appropriate reactions are triggered.

3) Reactions

Reactions are functions that automatically run whenever the observables they depend on change. These could be functions that update the UI or perform other side effects. Reactions help keep your application in sync with its state.

4) Computed Values

Computed values are derived from the state and other computed values. They are calculated on-demand and are automatically kept up to date as their dependencies change. Computed values are useful for complex calculations that depend on the state.

Let’s walk through a simple example of using MobX for state management in a React Native application

Suppose we’re building a counter app:

Setting Up MobX: Start by installing the required packages

npm install mobx mobx-react-lite

Creating an Observable: Define an observable for the counter value

import { makeObservable, observable, action } from 'mobx';

class CounterStore {
count = 0;

constructor() {
makeObservable(this, {
count: observable,
increment: action,
decrement: action,
});
}

increment() {
this.count++;
}

decrement() {
this.count--;
}
}

const counterStore = new CounterStore();
export default counterStore;

Using the Observable in Components: Now, you can use the counterStore in your components to manage the state

import React from 'react';
import { View, Text, Button } from 'react-native';
import { observer } from 'mobx-react-lite';
import counterStore from './counterStore';

const App = observer(() => {
return (
<View>
<Text>Count: {counterStore.count}</Text>
<Button title="Increment" onPress={counterStore.increment} />
<Button title="Decrement" onPress={counterStore.decrement} />
</View>
);
});

export default App;

Conclusion

MobX offers a straightforward way to manage the state in your React Native applications. By understanding the core concepts of observables, actions, reactions, and computed values, you can create more maintainable and reactive code. Whether you’re building a simple counter app or a complex application, MobX simplifies the process of state management and helps you focus on creating a great user experience.

You might be wondering already if you followed my previous articles which is the best between React Context and MobX

  • MobX: Choose MobX when you need automatic reactivity, fine-grained control over updates, and complex state management needs. It’s suitable for larger applications with intricate state structures and experienced developers.
  • React Context: Choose React Context when you’re looking for a simpler way to share the global state between components without extensive reactivity requirements. It’s a good choice for smaller to medium-sized applications and developers who want a more straightforward solution.

In summary, both MobX and React Context have their strengths. MobX provides powerful reactivity and is best suited for applications with complex state structures, while React Context offers a simpler way to share global states and is better suited for scenarios where advanced reactivity is not a primary concern. Your choice should be based on your application’s requirements and your familiarity with the concepts each solution offers.

You can find the full code in this GitHub Repo

--

--