Mobx
Mobx是一个功能强大,上手非常容易的状态管理工具。redux的作者也曾经向大家推荐过它,在不少情况下可以使用Mobx来替代掉redux。
这张图来自于官网,把这张图理解清楚了。基本上对于mobx的理解就算入门了。
官网有明确的核心概念使用方法,并配有egghead的视频教程。这里就不一一赘述了。
要特别注意当使用 mobx-react
时可以定义一个新的生命周期钩子函数 componentWillReact
。当组件因为它观察的数据发生了改变,它会安排重新渲染,这个时候 componentWillReact
会被触发。这使得它很容易追溯渲染并找到导致渲染的操作(action)。
-
componentWillReact
不接收参数 -
componentWillReact
初始化渲染前不会触发 (使用componentWillMount
替代) -
componentWillReact
对于 mobx-react@4+, 当接收新的 props 时并在setState
调用后会触发此钩子 -
要触发
componentWillReact
必须在render里面用到被观察的变量 -
使用Mobx之后不会触发
componentWillReceiveProps
1、搭建环境
mkdir my-app
cd my-app
npm init -y
npm i webpack webpack-cli webpack-dev-server -D
npm i html-webpack-plugin -D
npm i babel-loader @babel/core @babel/preset-env -D
npm i @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties -D
npm i @babel/plugin-transform-runtime -D
npm i @babel/runtime -S
npm i mobx -S
mkdir src
mkdir dist
touch index.html
touch src/index.js
touch webpack.config.js
编写webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
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: ['@babel/preset-env'],
plugins: [
//支持装饰器
["@babel/plugin-proposal-decorators", {
"legacy": true }],
["@babel/plugin-proposal-class-properties", {
"loose" : true }],
['@babel/plugin-transform-runtime']
]
}
}
}
]
},
plugins: [new HtmlWebpackPlugin()],
devtool: 'inline-source-map'
}
编写index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>