• Stars
    star
    180
  • Rank 213,097 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🎡 An extremely lightweight and responsive state management library

logo

npm NPM Version Coverage Status npm

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

$ npm install mota --save

Usage

Example 1: Hello Mota

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

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

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

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

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

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

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>;
  }
}

More Repositories

1

mditor

📝 [ M ] arkdown + E [ ditor ] = Mditor
JavaScript
535
star
2

safeify

📦 Safe sandbox that can be used to execute untrusted code.
TypeScript
487
star
3

cize

🔌 Continuous integration with the simplest solution
JavaScript
99
star
4

faked

faked 是一个在前端开发中用于模拟服务端接口的模块。
JavaScript
67
star
5

gq-loader

The best GraphQL Loader for Webpack
JavaScript
59
star
6

shadow-view

🦇针对 React 的 Shadow DOM 容器组件
TypeScript
56
star
7

hoster

Manage your system's hosts configuration and switch easily
JavaScript
50
star
8

confman

一个强大的配置文件加载器
JavaScript
33
star
9

iweb

一个用于前端开发的实时预览工具
JavaScript
21
star
10

mokit

A minimal mvvm framework
JavaScript
19
star
11

lockman

一个用于多进程的并发控制锁
JavaScript
16
star
12

react-finger

👉 React Finger is a library of gesture events for React that allows developers to use a single set of events for both desktop and mobile devices.
TypeScript
14
star
13

tp

一个 “轻量,简洁,高效” 的 javascript 模板引擎!
JavaScript
12
star
14

doczilla

The fastest and easy-to-use document generation tool
JavaScript
10
star
15

ems

Easy Module System: 简洁、易用的模块系统
JavaScript
8
star
16

channeler

【Channeler】Browser 多窗口跨域通讯 & Node 多进程通讯
TypeScript
7
star
17

stp

基于 es6 模板字符串的超简模板引擎
JavaScript
7
star
18

vmodule-webpack-plugin

A virtual module webpack plugin
TypeScript
7
star
19

mota-gesture

🏌️‍♂️基于 Mota 同时兼容 PC & Mobile 的事件扩展
TypeScript
6
star
20

noka

🐬A Web Development Framework Based on IoC
JavaScript
5
star
21

sditor

🌿 Sditor = [ S ] chema + E [ Ditor ] -> JSON Schema Editor
TypeScript
5
star
22

mota-form

🧱基于 Mota & Mota Validation 的非常好用的表单组件
TypeScript
5
star
23

cmdline

cmdline is a process.argv parser
TypeScript
4
star
24

mota-old

mota 是一个主张「面向对象」的 React 应用辅助库
JavaScript
4
star
25

textor

This is a very cool text editor, not a IDE, Only 3s to start using.
HTML
4
star
26

yamlql

TypeScript
4
star
27

react-best-router

TypeScript
3
star
28

ober

TypeScript
3
star
29

oneport

用于获取一个空闲的端口的小模块
JavaScript
3
star
30

mota-validation

面向 React 状态管理库 Mota 的专用验证插件
TypeScript
3
star
31

react-turn

📽React Turn 是一个「功能强、体积小、易使用」的「转场动画」组件
CSS
3
star
32

dn-template-mota

JavaScript
3
star
33

smache

Smart caching module
JavaScript
2
star
34

ntils

一个 Node 工具函数集
TypeScript
2
star
35

eify

TypeScript
2
star
36

csvx

Simple and easy to use CSV parser
JavaScript
2
star
37

react-dock-panel

TypeScript
2
star
38

CodeMirror-React

JavaScript
2
star
39

mokit-touch

mokit touch api
JavaScript
1
star
40

mokit-app

mokit app template
JavaScript
1
star
41

mokit-router

mokit router plugin
JavaScript
1
star
42

fc2koa

TypeScript
1
star
43

hilog

A lightweight and high-quality log Library
TypeScript
1
star
44

mokit-cli

mokit command line interface
JavaScript
1
star
45

cmenu

TypeScript
1
star
46

Amuse

This is an easy to use Ioc framework
C#
1
star
47

dn-template-mota-hook

JavaScript
1
star
48

mov

Extremely lightweight tween animation library
TypeScript
1
star
49

TpNet

tp.js ( for .net /mono)
C#
1
star
50

mokit-transition

mokit-transition
CSS
1
star
51

ppt.js

JavaScript
1
star
52

classname-loader

JavaScript
1
star
53

cify

JavaScript
1
star
54

WebBuilder-ST

一个为Sublime Text2而开发的JS+CSS批量压缩插件(需要Mono或.NET)
JavaScript
1
star
55

shify

JavaScript
1
star
56

Snake

一个轻量的脚本语言
C++
1
star