前提是你已经搭好环境,而且能跑起来;
不然的话先进行:Mac环境搭建,目标:ios javascript
本项目使用的依赖:java
"dependencies": { "lodash": "^4.17.5", "mobx": "^4.1.0", "mobx-react": "^5.0.0", "native-base": "^2.3.5", "react": "^16.3.0-alpha.1", "react-native": "0.54.4", "react-navigation": "^1.5.8" },
npm i mobx mobx-react --save
npm i babel-plugin-transform-decorators-legacy babel-preset-react-native-stage-0 --save-dev
touch .babelrc
写入:react
{ 'presets': ['react-native'], 'plugins': ['transform-decorators-legacy'] }
补充:ES7装饰器语法在编译器可能会报错;我这里用的是vscode,分享解决办法:ios
"javascript.implicitProjectConfig.experimentalDecorators": true
至此准备工做已经差很少了,接下来写代码。。。npm
store
文件夹;store
下新建index.js
做用是合并每一个store
到一个store
中去,最终经过 <Provider {...store}>
方式注入<App/>
;store
下新建几个js,示例:// home import { observable, action } from "mobx"; class HomeStore { @observable text; // 注册变量,使其成为可检测的 @observable num; constructor() { this.num = 0; // 初始化变量,能够定义默认值 this.text = "Hello, this is homePage!!!"; } @action // 方法推荐用箭头函数的形式 plus = () => { this.num += 1; }; @action minus = () => { this.num -= 1; }; } const homeStore = new HomeStore(); export { homeStore };
// user import { observable, action } from "mobx"; class UserStore { @observable userInfo; @observable text; constructor() { this.userInfo = "123"; this.text = "Hello, this is UserPage!!!"; } @action getListData = () => { fetch(`http://yapi.demo.qunar.com/mock/5228/record/list`) .then( action("fetchRes", res => { return res.json(); }) ) .then( action("fetchSuccess", data => { return (this.userInfo = data); }) ) .catch( action("fetchError", e => { console.log(e.message); }) ); }; } const userStore = new UserStore(); export { userStore };
import { homeStore } from "./home"; import { saleStore } from "./sale"; import { userStore } from "./user"; const store = { homeStore, saleStore, userStore }; export default store;
js目录下新建tabs文件夹,新建HomeScreen.jsjson
import React, { Component } from "react"; import { View, Text, StyleSheet, TouchableOpacity } from "react-native"; import { connect } from "mobx-react"; import { observer, inject } from "mobx-react"; import { Button, Container } from "native-base"; import Headers from "../common/components/header"; @inject(["homeStore"]) // 注入对应的store @observer // 监听当前组件 class HomeScreen extends Component { constructor(props) { super(props); this.store = this.props.homeStore; //经过props来导入访问已注入的store this.state = { }; } render() { const { text, num } = this.store; return ( <Container> <Headers title="首页" type='index' navigation={this.props.navigation} /> <Text>{text}</Text> <Button primary onPress={() => this.store.plus()}> <Text>add</Text> </Button> <Text>{num}</Text> <Button primary onPress={() => this.store.minus()}> <Text>Minu</Text> </Button> </Container> ); } } export default HomeScreen; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "center", alignItems: "center", backgroundColor: "#F5FCFF" } });
store
,接下来注入store
;在初始化的项目结构中,项目运行的入口文件是index.js
,注册了同级目录下的App.js
;segmentfault
// index.js import { AppRegistry } from 'react-native'; import App from './App'; AppRegistry.registerComponent('demo', () => App);
因为须要在App.js
上套入store
因此改写结构,react-native
App.js
touch App.js
api
能够把根目录下App.js
的内容copy过来,而后删掉根目录下App.js
文件;babel
setup.js
文件;import React from "react"; import { Provider } from "mobx-react"; import App from "./App"; import store from "./store"; export default function setup() { class Root extends React.Component { render() { return ( <Provider {...store}> <App /> </Provider> ); } } return Root; }
import { AppRegistry } from 'react-native'; import setup from "./js/setup"; AppRegistry.registerComponent('******', () => setup());
如今store
已经注入,在每一个组件中也能够拿到当前store
的数据了;能够进行代码开发了;