Mbox is a lightweight state management library designed to simplify state management in React applications, particularly suited for small to medium-sized projects. Mbox provides an intuitive way to manage global state, leveraging MobX and React Hooks to make state management simple and efficient.
React Mbox Basic Usage
Installing Mbox
First, install Mbox in your project using npm or yarn:
npm install mbox-react
# Or
yarn add mbox-reactCreating a State Model
Mbox uses MobX’s observable objects to create state models. A state model typically includes state, actions, and computed properties.
import { makeAutoObservable } from 'mobx';
import { MBox } from 'mbox-react';
class CounterModel {
count = 0;
constructor() {
makeAutoObservable(this);
}
increment = () => {
this.count += 1;
};
decrement = () => {
this.count -= 1;
};
}
const counterModel = new CounterModel();
MBox.registerModel(counterModel);Using Mbox Provider
To enable components to access the state model, wrap the application’s root component with MBoxProvider.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { MBoxProvider } from 'mbox-react';
ReactDOM.render(
<MBoxProvider>
<App />
</MBoxProvider>,
document.getElementById('root')
);Using State in Components
Mbox provides the useModel Hook to access state models in components.
import React from 'react';
import { useModel } from 'mbox-react';
function Counter() {
const counterModel = useModel('counterModel');
return (
<div>
<h1>{counterModel.count}</h1>
<button onClick={counterModel.increment}>+</button>
<button onClick={counterModel.decrement}>-</button>
</div>
);
}Observing State Changes
Mbox allows components to listen for state changes, automatically re-rendering when the state updates.
import React from 'react';
import { useModel } from 'mbox-react';
function Counter() {
const counterModel = useModel('counterModel');
return (
<div>
<h1>{counterModel.count}</h1>
{/* This component re-renders automatically when counterModel.count changes */}
<button onClick={counterModel.increment}>+</button>
<button onClick={counterModel.decrement}>-</button>
</div>
);
}Asynchronous Operations
Mbox supports asynchronous operations, allowing the use of async/await in actions.
class CounterModel {
// ...
async fetchCount() {
const response = await fetch('/api/count');
const data = await response.json();
this.count = data.count;
}
}
// Call in a component
async function Counter() {
const counterModel = useModel('counterModel');
await counterModel.fetchCount();
return (
// ...
);
}Using Decorators
Mbox supports decorators to define state models, enhancing code readability.
import { observable, action } from 'mobx';
import { MBox } from 'mbox-react';
@MBox.model
class CounterModel {
@observable count = 0;
@action
increment = () => {
this.count += 1;
};
@action
decrement = () => {
this.count -= 1;
};
}
MBox.registerModel(new CounterModel());Mbox Advanced Features
Using the @computed Decorator
In addition to @observable and @action, Mbox supports the @computed decorator for creating properties that depend on other observable properties. This allows you to compute and cache complex data without burdening components.
import { observable, computed } from 'mobx';
import { MBox } from 'mbox-react';
@MBox.model
class CounterModel {
@observable count = 0;
@computed get doubleCount() {
return this.count * 2;
}
}Multi-Model Management
For large applications, you may need to manage multiple state models. Mbox supports registering and using multiple models.
@MBox.model
class UserPreferences {
@observable theme = 'light';
}
@MBox.model
class Cart {
@observable items = [];
}
MBox.registerModel(new UserPreferences());
MBox.registerModel(new Cart());In components, access models by specifying their names with the useModel Hook.
const userPrefs = useModel('UserPreferences');
const cart = useModel('Cart');Compatibility with Redux Middleware
Although Mbox is based on MobX, it can work with Redux middleware like redux-thunk or redux-saga, enhancing your application’s asynchronous processing capabilities.
Mbox Extensions and Customization
Extending Mbox Functionality
Mbox’s core design allows developers to extend its functionality based on their needs, such as adding logging, error handling, or custom middleware. This can be achieved by adding methods or properties to models.
@MBox.model
class CounterModel {
@observable count = 0;
logAction(actionName) {
console.log(`Action: ${actionName} called`);
}
@action
increment = () => {
this.logAction('increment');
this.count += 1;
};
}Custom Middleware
While Mbox does not have a built-in middleware system, you can implement similar functionality using custom higher-order components (HOCs) or the useEffect Hook.
import React, { useEffect } from 'react';
import { useModel } from 'mbox-react';
const withLogging = (WrappedComponent) => {
return (props) => {
const model = useModel('CounterModel');
useEffect(() => {
model.logAction('Component Mounted');
return () => {
model.logAction('Component Unmounted');
};
}, []);
return <WrappedComponent {...props} />;
};
};



