test.md# 主要内容css
为了以后项目的拓展性,将全部的数据集中放在 store/
目录下。如今项目结构以下:react
src/
App.js
App.css
index.js
conponents/
(some components)
containers/
(some containers)
stores/
(some stores)
复制代码
先看一下如何组织 store。目前是按照功能来组织,如今只开发了记分功能,因此只有一个 scoreStore
,后续还会有 userStore
来管理和用户登陆相关的数据,等等。git
在 scoreStore.js
中定义了以下 observable 和 action:github
import { observable, action } from 'mobx';
class ScoreStore {
// 数据
@observable score = 0;
// 方法
@action setScore (score) {
this.score = score
}
}
export default new ScoreStore();
复制代码
在 HomePage
中使用则须要在前面 inject 这个 store,并加上 observer 修饰:架构
// HomePage.js
// 一系列导入中加上 mobx-react 相关
import { inject, observer } from "mobx-react";
@inject ("scoreStore")
@observer
class HomePage extends Component {
// 一些代码…
// 访问 scoreStore 的数据或方法时时使用 this.props
this.props.scoreStore.setScore(score);
// 更多代码…
}
复制代码
其余须要访问数据的组件也同理作修改,这样,在修改数据的时候,全局都会相应变化了。app
github.com/gothinkster… 主要参考了 store 的结构。post
React + MobX + Electron + Node.js + MongoDB 全栈项目开发实践(零)前言this
React + MobX + Electron + Node.js + MongoDB 全栈项目开发实践(一)spa
React + MobX + Electron + Node.js + MongoDB 全栈项目开发实践(二)容许 decorator3d