# Mobx

* declarative, reactive programming
* different paradigm then redux/zustand

### Diff from Zustand/Redux

* Instead of selectors, just wrap functional component in observers if it relies on state
* Need to pass store down yourself either just directly or useContext

// store.js

```ts
import { makeAutoObservable } from "mobx";

class CounterStore {
    value = 0;

    constructor() {
        makeAutoObservable(this);
    }

    increment() {
        this.value++;
    }
}

export const counterStore = new CounterStore();

```

// CounterComponent.js

```ts
import React from "react";
import { observer } from "mobx-react-lite";
import { counterStore } from "./store";

const Counter = observer(() => (
    <div>
        <p>Count: {counterStore.value}</p>
        <button onClick={() => counterStore.increment()}>Increment</button>
    </div>
));

export default Counter;
```

### Advanced

* **`autorun`**: Automatically tracks any observable accessed within its function and re-executes when they change.
* **`reaction`**: Tracks a specific observable and runs side-effects when it changes.
* **`when`**: Executes a function when a specific condition becomes true.

```ts
reaction(
    () => store.user.name, // Observe changes to user's name
    (name) => console.log(`User's name is now ${name}`) // Side effect
);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openai.gitbook.io/code-cheatsheets/react/state_management/mobx.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
