# mobx-demo **Repository Path**: gl-web/mobx-demo ## Basic Information - **Project Name**: mobx-demo - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-23 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 环境搭建 1. 安装 `webpack babel` 支持 ``` yarn add webpack webpack-cli @babel/preset-env @babel/core babel-loader @babel/plugin-proposal-class-properties ``` 2. 配置 `webpack.config.js` ``` const path = require('path'); const config = { mode: 'development', entry: path.resolve(__dirname, 'src/index.jsx'), output: { path: path.resolve(__dirname, 'dist'), filename: 'main.js' }, module: { rules: [ { test: /\.jsx$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: [ '@babel/preset-env' ], plugins: [ ["@babel/plugin-proposal-class-properties", { "loose": true }] ] } } } ] }, devtool: 'inline-source-map' }; module.exports = config; ``` 3. 安装装饰器支持 ``` yarn add @babel/plugin-proposal-decorators ``` 4. 配置 `webpack.config.js` ``` const path = require('path'); const config = { mode: 'development', entry: path.resolve(__dirname, 'src/index.jsx'), output: { path: path.resolve(__dirname, 'dist'), filename: 'main.js' }, module: { rules: [ { test: /\.jsx$/, 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 }] ] } } } ] }, devtool: 'inline-source-map' }; module.exports = config; ``` 5. 安装 `react react-dom prop-types` 支持 ``` yarn add react react-dom prop-types ``` 6. 配置 `webpack.config.js` ``` const path = require('path'); const config = { mode: 'development', entry: path.resolve(__dirname, 'src/index.jsx'), output: { path: path.resolve(__dirname, 'dist'), filename: 'main.js' }, module: { rules: [ { test: /\.jsx$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: [ '@babel/preset-env', '@babel/preset-react' ], plugins: [ ["@babel/plugin-proposal-decorators", { "legacy": true }], ["@babel/plugin-proposal-class-properties", { "loose": true }] ] } } } ] }, devtool: 'inline-source-map' }; module.exports = config; ``` 7. 安装 `mobx mobx-react` ``` yarn add mobx mobx-react ``` ### Mobx 要点 1. 定义状态并使其可观察 `@observable` 示例 ``` class Todo { id = Math.random(); @observable title = ''; @observable finished = false; constructor(title) { this.title = title; } } class Store { @observable todos = []; } ``` 2. 创建视图以响应状态的变化 `@observer` 示例 ``` const store = new Store(); @observer class TodoList extends Component { static propTypes = { store: PropTypes.shape({ createTodo: PropTypes.func, todos: ObservablePropTypes.observableArrayOf(ObservablePropTypes.observableObject).isRequired }).isRequired } render() {trace(); const { store } = this.props; const { todos } = store; // console.log('TodoList', this.props); return (