# mota

> An extremely lightweight and responsive state management library

Latest version **8.2.7** (published 2024-02-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install mota
pnpm add mota
yarn add mota
bun add mota
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.2.7 |
| Published | 2024-02-15 |
| First published | 2017-10-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 636.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 180 |
| Author | Houfeng |
| Maintainers | houzhanfeng |

## Links

- npm: https://www.npmjs.com/package/mota
- Repository: https://github.com/Houfeng/mota
- Homepage: https://github.com/Houfeng/mota#readme
- Issues: https://github.com/Houfeng/mota/issues
- npm.io page: https://npm.io/package/mota

## Dependencies (2)

- [ober](https://npm.io/package/ober.md) 8.3.3
- [tslib](https://npm.io/package/tslib.md) *

## Recent versions

- 8.2.7 (latest) — 2024-02-15
- 8.0.0-alpha.20 (alpha) — 2022-05-17
- 5.0.0 (next) — 2020-05-14
- 8.2.6 — 2023-10-11
- 8.2.5 — 2023-10-11
- 8.2.4 — 2023-10-08
- 8.2.3 — 2022-11-17
- 8.2.2 — 2022-11-17
- 8.2.1 — 2022-11-08
- 8.2.0 — 2022-10-22
- 8.1.17 — 2022-08-11
- 8.1.16 — 2022-06-29
- 8.1.15 — 2022-06-29
- 8.1.14 — 2022-06-29
- 8.1.13 — 2022-06-24
- … 235 more at https://npm.io/package/mota/versions

## README

![logo](http://houfeng.net/mota/logo.jpg)

<div align="center">

[![npm](https://img.shields.io/npm/l/mota.svg)](LICENSE.md)
[![NPM Version](https://img.shields.io/npm/v/mota.svg)](https://www.npmjs.com/package/mota)
[![Coverage Status](https://coveralls.io/repos/github/Houfeng/mota/badge.svg?branch=master)](https://coveralls.io/github/Houfeng/mota?branch=master)
[![npm](https://img.shields.io/npm/dt/mota.svg)](https://www.npmjs.com/package/mota)
<!-- [![Build Status](https://www.travis-ci.org/Houfeng/mota.svg?branch=master)](https://www.travis-ci.org/Houfeng/mota) -->

</div>

# Overview

Mota is a "lightweight and responsive" state management library, which is less than 5KB. Developers can use it to write "almost framework independent pure js/ts business models", and then use Mota to simply let react components respond to model changes.

# Install

Install through NPM as follows
```sh
$ npm install mota --save
```

# Usage

**Example 1**: Hello Mota

```jsx
import { observable, observer } from "mota";

const model = observable({ count: 0 });
const add = ()=>model.count++;

const View1 = observer(() => {
  return <div>{model.count}</div>;
});

const View2 = observer(() => {
  return <div>
    <span>{model.count}</span>
    <button onClick={add}>click<button>
  </div>;
});
```

**Example 2**: Using useObservable

```jsx
import { observer, useObservable } from "mota";

const View = observer(() => {
  const model = useObservable({ count: 0 });
  return <div>
    <span>{model.count}</span>
    <button onClick={()=>model.count++}>click<button>
  </div>;
});
```

**Example 3**: Using useComputed

```jsx
import { observer, observable, useComputed } from "mota";

const user = observable({ 
  firstName: 'Feng',
  lastName: 'Hou'
});

const View = observer(() => {
  // The fullName will be cached and responsive
  const fullName = useComputed(()=>{
    return `${user.firstName} ${user.lastName}`;
  });
  return <div>{fullName}</div>;
});
```


**Example 4**: Using useAutoRun

```jsx
import { observer, observable, useAutoRun } from "mota";

const model = observable({ count: 0 });

const View = observer(() => {
  // When the count changes, 
  // it will be automatically re executed and printed 'count: n'
  useAutoRun(()=>{
    console.log('count:', model.count);
  });
  return <div>{model.count}</div>;
});
```

**Example 5**: Using useWatch

```jsx
import { observer, observable, useWatch } from "mota";

const model = observable({ count: 0 });

const View = observer(() => {
  // When the result of the evaluation function changes,
  // the processing function is re executed.
  useWatch(()=>model.count%10, (oldValue, newValue)=>{
    console.log(`old: ${oldValue}, new: ${newValue}`);
  });
  return <div>{model.count}</div>;
});
```

**Example 6**: Using in class components

```jsx
import { observer, observable, autorun, watch } from "mota";

const model = observable({ count: 0 });

@observer
class View extends React.Component {
  add = ()=> model.count++;

  componentDidMount(){
    this.destroyAutoRun = autorun(()=>{
      console.log('autorun count: ', model.count);
    });
    this.destroyWatch = watch(()=> model.count%10, ()=>{
      console.log('autorun count: ', model.count);
    });
  }

  componentWillUnmount(){
    this.destroyAutoRun();
    this.destroyWatch();
  }

  @computed get message(){
    return `count: ${model.count}`;
  }

  render() {
    return <div>
      <span>{this.message}</span>
      <button onClick={this.add}>click<button>
    </div>;
  }
}
```

**Example 7**: Using multiple instances in class components

```jsx
import { observer, observable, autorun, watch } from "mota";

@observable
class Model {
  count = 0;
  add () {
    this.count++;
  }
  @computed get message(){
    return `count: ${model.count}`;
  }
}

@observer
class View extends React.Component {
  model = new Model();
  render() {
    return <div>
      <span>{this.model.message}</span>
      <button onClick={()=>this.model.add()}>click<button>
    </div>;
  }
}
```

---
_Source: https://npm.io/package/mota · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
