目录
Mobx接入实战
Mobx构造复杂应用需要注意的
Mobx5 Or Mobx4
Mobx5
Mobx4
@/package.json
@/src/routes/index.jsx
@/src/app.jsx
@/src/index.jsx
@/src/models/home/index.js
@/src/models/index.js
@/src/containers/home/index.jsx
Mobx VS Redux
对状态变更更为细腻,Mobx更适合局部场景
// package.json
{
"name": "react-mobx-demo",
"version": "1.0.0",
"description": "react mobx feature",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --hot --color --progress --host localhost --config webpack.config.js --env.dev",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/luzuoquan/react-mobx-demo.git"
},
"keywords": [
"react",
"mobx"
],
"author": "luzuoquan",
"license": "MIT",
"bugs": {
"url": "https://github.com/luzuoquan/react-mobx-demo/issues"
},
"homepage": "https://github.com/luzuoquan/react-mobx-demo#readme",
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-decorators": "^7.8.3",
"@babel/plugin-proposal-export-default-from": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/preset-react": "^7.9.4",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"eslint": "6.8.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-import-resolver-webpack": "^0.12.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.20.0",
"eslint-plugin-react-hooks": "3",
"html-webpack-plugin": "^4.3.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"@babel/runtime": "^7.9.6",
"core-js": "3",
// mobx
"mobx": "^5.15.4",
"mobx-react": "^6.2.2",
// 类型规划
"prop-types": "^15.7.2",
// react
"react": "^16.13.1",
"react-dom": "^16.13.1",
// router
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"regenerator-runtime": "^0.13.5",
"whatwg-fetch": "^3.0.0"
}
}
import React from 'react';
import {
BrowserRouter,
Route,
Switch,
} from 'react-router-dom';
import Home from '@/containers/home';
export default function () {
return (
);
}
import React from 'react';
import Routes from '@/routes';
export default () => ;
import React from 'react';
import { render } from 'react-dom';
// 全局变量注入
import { Provider } from 'mobx-react';
// 全局store
import store from './models';
import App from './app';
render(
,
document.querySelector('#app')
);
import { observable, action } from 'mobx';
export class HomeInfo {
@observable name = '云课堂';
@action
updateName = (params) => {
// Mobx中可以直接修改
this.name = params;
};
}
export default new HomeInfo();
import homeInfo from './home';
export default {
homeInfo,
};
import { observer, inject } from 'mobx-react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
// 全局变量注入
@inject('homeInfo')
// 关联组件
@observer
export default class Home extends Component {
static propTypes = {
homeInfo: PropTypes.objectOf(PropTypes.object),
};
static defaultProps = {
homeInfo: {},
};
handleClick = () => {
const {
homeInfo: { updateName },
} = this.props;
updateName('Mobx接入实战 - 云课堂');
};
render() {
const {
homeInfo: { name },
} = this.props;
return (
{name}
);
}
}
Mobx:适合小而简单的工程
Redux:适合大型应用