# React

Using ActiveJS with React is pretty straightforward since ActiveJS has the same basic API as every other subscription-based State Management solution. If you're following the [general guidelines](/activejs/guides/code-style.md), then you are already on the right path.

Apart from the following examples, you will find more examples in the [Examples](/activejs/more/examples.md) section.

## Examples:

### 1. Counter using Hooks

Here's an example of implementing a simple **counter** with ActiveJS [NumUnit](/activejs/fundamentals/units/numunit.md) and **React Hooks**.

```typescript
// initialize a NumUnit.
const counterUnit = new NumUnit() // with default value 0

// define two pure functions that produce a new value
const increment = value => value + 1;
const decrement = value => value - 1;

// create a react component
function App() {
  const [value, setValue] = useState<number>(); // create local state

  // use an effect to update the local state when counterUnit's value changes
  useEffect(() => {
    const subscription = counterUnit.subscribe(setValue);
    return () => subscription.unsubscribe(); // prevent memory leak
  }, []);

  return (
    <React.Fragment>
      Counter Value: {value}

      <button onClick={() => counterUnit.dispatch(increment)}>Increase</button>
      <button onClick={() => counterUnit.dispatch(decrement)}>Decrease</button>
    </React.Fragment>
  );
}
```

Try it out on [StackBlitz](https://stackblitz.com/edit/react-ts-deakuh?file=index.tsx).

### 2. Counter using [useUnit](/activejs/integrations/react/useunit-hook.md) Hook

Here's the same **counter** example using the [useUnit](/activejs/integrations/react/useunit-hook.md) Hook. This is much better than replicating the same `useState` and `useEffect` logic every time you want to use a Unit.

```typescript
// initialize a NumUnit.
const counterUnit = new NumUnit() // with default value 0

// define two pure functions that produce a new value
const increment = value => value + 1
const decrement = value => value - 1

// create a react component
function App() {
  const {value, dispatch, resetValue} = useUnit(counterUnit)

  return (
    <React.Fragment>
      Counter Value: {value}

      <button onClick={() => dispatch(increment)}>Increase</button>
      <button onClick={() => dispatch(decrement)}>Decrease</button>
      <button onClick={() => resetValue()}>Reset</button>
    </React.Fragment>
  )
}
```

Try it out on [StackBlitz](https://stackblitz.com/edit/react-ts-bf5mxu?file=index.tsx).

### 3. Counter using [useObservable](/activejs/integrations/react/useobservable-hook.md) Hook

Here's the same **counter** example again, using the [useObservable](/activejs/integrations/react/useobservable-hook.md) Hook. This is even better than [useUnit](/activejs/integrations/react/useunit-hook.md), since we don't have to re-map the Unit's API again in a Hook, we just need the dynamic value to trigger rendering update, and for `dispatch` and other methods, we can use the Unit directly.

```typescript
// initialize a NumUnit.
const counterUnit = new NumUnit() // with default value 0

// define two pure functions that produce a new value
const increment = value => value + 1;
const decrement = value => value - 1;

// create a react component
function App() {
  const value = useObservable(counterUnit);

  return (
    <React.Fragment>
      <p>
        Counter Value: <b>{value}</b>
      </p>

      <button onClick={() => counterUnit.dispatch(increment)}>Increase</button>
      <button onClick={() => counterUnit.dispatch(decrement)}>Decrease</button>
      <button onClick={() => counterUnit.resetValue()}>Reset</button>
    </React.Fragment>
  );
}
```

Try it out on [StackBlitz](https://stackblitz.com/edit/react-ts-iifk5z?file=index.tsx).


---

# 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://docs.activejs.dev/activejs/integrations/react.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.
