mobx
状态管理工具
VS Redux
开发难度低:redux函数式编程思想;mobx响应式编程风格,同时集成度高,避免引入众多第三方库
开发代码量少:redux有reducer等众多概念,而mobx只要在store中更新即可
渲染性能好:redux通过shouldComponentUpdate优化,但是当应用大到一定程度,就比较难实现;mobx精确的指出了哪些需要重复渲染,将re-render限制在最小范围之内
mobx核心思想
状态变化引起的副作用应该被自动触发
数据流:类redux单向数据流模式(action =》 State =〉Reaction)
基础知识
实现继承和多态
mkdir mobx_testcd mobx_testmkdir srctouch src/index.jsnpm init -ytouch webpack.config.jsyarn add webpack webpack-cli babel-core babel-preset-env babel-loader -D
配置webpack.config.js
const path = require('path')module.exports = {mode: 'development', // 'production'entry: path.resolve(__dirname, 'src/index.js'),output: {path: path.resolve(__dirname, 'dist'),filename: 'main.js',},module: {rules: [{test: /\.js$/,exclude: /node_modules/,use: {loader: 'babel-loader',options: {presets: ['env'],}}}]},devtool: 'inline-source-map', // 方便调试}
配置package.json
"scripts": {"start": "webpack -w" // -w:js文件更改自动执行编译},
编写index.html
<!DOCTYPE html><html><head><title></title></head><body><script src= "src/index.js" type="text/javascript"></script></body></html>
编写src/index.js 用原生实现继承和多态
继承:Dog.prototype.proto === Animal.prototype
多态:重写Dog的name属性
Dog的原型对象的构造函数应该指向Dog而不是Animal
function Animal() {// body...}function Dog() {// body...}Object.defineProperties(Animal.prototype, {name: {value() {return 'Animal'}},say: {value() {return `I'm ${this.name()}`}}})// dog instanceOf Animal === true// dog.__proto__.__proto__ === Animal.prototype// dog.__proto__ === Dog.prototype// Dog.prototype.__proto__ === Animal.prototypeDog.prototype = Object.create(Animal.prototype, {constructor: {value: Dog,enumerable: false,},name: {value() {return `Dog`}}})// document.write(new Dog() instanceOf Animal())// document.write(new Dog().say())document.write(Dog.prototype.constructor)
通过ES6来实现
class Animal {name() {return 'Animal'}say() {return `I'm ${this.name()}`}}class Dog extends Animal {name() {return 'Dog'}}console.log(new Dog() instanceOf Animal
安装babel插件,使其支持在类中声明成员变量yarn add babel-plugin-transform-class-properties -D
配置webpack.config.js
module.rules下面的use.plugins: [‘transform-class-properties’]
decorator
Decorator是在声明阶段实现类与类成员注解的一种语法.本质上是特殊的函数。
安装babel插件yarn add babel-plugin-transform-decorators-legacy -D
配置webpack.config.js
module.rules下面的use.plugins: [‘transform-decorators-legacy’]
mobx 常用 API
安装mobx依赖:yarn add mobx
observable(可观察的数据)
是一种让数据的变化可以被观察的方法
哪些数据可以被观察:
原始类型:String | Number | Boolean | Symbol
对象
数组
PS:一定要检查数组的长度以避免越界访问,在mobx中越界的数组值是不被观察的.同样要检查对象是否含有属性hasOwnProperty
import { observable } from 'mobx';class Store {@observable array = []@observable obj = {}@observable map = new Map()@observable string = 'hello'@observable number = 20@observable bool = false}
对可观察的数据作出反应
方式:
computed
observe()
get()
autorun
when
reaction
mobx 实现 todoList
yarn add mobx-react react react-dom prop-types
yarn add babel-preset-react
配置webpack.config.jspresets: [['env', 'react']],
资源
mobx-state-tree
