React Native Flux框架的使用
Flux是RN比較常用的框架,用起來相對比較簡單和方便侮邀。Flux實現(xiàn)的原理不再說明。這里只做Flux在項目中的具體使用。
使用Flux首先需要安裝Flux包
npm install flux
創(chuàng)建flux文件
- 創(chuàng)建
dispatcher.js
-> dispatcher
var Dispatcher = require('flux').Dispatcher;
module.exports = new Dispatcher();
- 聲明
type
常量AppConstants.js
npm install keymirror
keymirror: 在 Closure Compiler 的高級模式下搀缠,Object 的 key 會被壓縮替換成更短的字符,這樣就不能創(chuàng)建一個 key 跟 value 相等的 Object 了近迁。keyMirror 就是解決這個問題的艺普。
import keyMirror from 'keymirror'; [^keymirror]
let AppConstants = keyMirror({
ADD_NUMBER: null,
GET_NUMBER: null,
});
export default AppConstants;
- 創(chuàng)建
AppActions.js
-> action
import AppDispatcher from '../dispatcher/AppDispatcher';
import AppConstants from '../constants/AppConstants';
let AppActions = {
addNumberAction(num) {
// 加上num
AppDispatcher.dispatch({
actionType: AppConstants.ADD_NUMBER,
num: num
});
},
getNumberAction(){
//獲取最新的數(shù)組
AppDispatcher.dispatch({
actionType:AppConstants.GET_NUMBER,
})
}
};
export default AppActions;
- 創(chuàng)建
AppStore
-> store
import {EventEmitter} from 'events';
import assign from 'object-assign';
import AppDispatcher from '../dispatcher/AppDispatcher';
import AppConstants from '../constants/AppConstants';
let CHANGE_EVENT = 'change';
const AppStore = assign({}, EventEmitter.prototype, {
num: 100, //初始化
addNumber: function(number) {
this.num += number;
// alert(this.num);
return this.num;
},
getNumber: function() {
return this.num;
},
emitChange: function() {
this.emit(CHANGE_EVENT);
},
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
})
// Register callback to handle all updates
AppDispatcher.register((action) =>{
switch(action.actionType) {
case AppConstants.ADD_NUMBER:
// _getStateMessage(action.num);
AppStore.addNumber(action.num);
AppStore.emitChange()
//alert(action.num);
break;
case AppConstants.GET_NUMBER:
AppStore.getNumber();
AppStore.emitChange()
break;
case AppConstants.SET_MESSAGE:
AppStore.emitChange();
break;
default:
}
});
export default AppStore;
在項目中使用
此項目實現(xiàn)效果為A頁面數(shù)字 Push到 B 頁面 B頁面對數(shù)字進行添加 pop回A頁面 A頁面數(shù)字改變
A.js
import React, { Component } from 'react'
import {
Navigator,
Text,
View,
AsyncStorage,
TouchableHighlight
} from 'react-native'
import SecondPage from './SecondPage'
import AppStore from './stores/AppStore'
// import AppActions from './actions/AppActions'
export default class HomePage extends Component
{
constructor(props) {
super(props);
this.state = {
number:100
}
}
componentDidMount() {
AppStore.addChangeListener(this._onChange.bind(this));
}
componentWillUnmount() {
AppStore.removeChangeListener(this._onChange.bind(this));
}
_onChange() {
this.setState({
number: AppStore.getNumber()
});
}
render(){
return (
<View style={{flex:1,justifyContent:'center',alignItems: 'center'}}>
<Text style={{padding:50}}>{this.state.number}</Text>
<TouchableHighlight onPress={()=>{
const navigator = this.props.navigator
if (navigator) {
navigator.push(
{
name:'SecondPage',
component:SecondPage,
params:{
getRegionSelectText:(text)=>{
this.setState({regionSelect:text})
}
}
}
)
}
}}>
<Text>下一個頁面</Text>
</TouchableHighlight>
</View>
)
}
}
B.js
import React, { Component } from 'react'
import {
Navigator,
Text,
View,
AsyncStorage,
TouchableHighlight
} from 'react-native'
import AppStore from './stores/AppStore'
import AppActions from './actions/AppActions'
export default class SecondPage extends Component
{
constructor(props) {
super(props);
this.state = {
number:AppActions.getNumberAction()
}
}
componentDidMount() {
AppStore.addChangeListener(this._onChange.bind(this));
}
componentWillUnmount() {
AppStore.removeChangeListener(this._onChange.bind(this));
}
_onChange() {
this.setState({
number: AppStore.getNumber()
});
}
render(){
return (
<View style={{flex:1,justifyContent:'space-around',alignItems: 'center',backgroundColor:'#eee'}}>
<Text>{this.state.number}</Text>
<TouchableHighlight onPress={()=>{
AppActions.addNumberAction(10);
}}>
<Text>增加</Text>
</TouchableHighlight>
<TouchableHighlight onPress={()=>{
const navigator = this.props.navigator
if (navigator) {
navigator.pop()
}
}}>
<Text>返回頁面</Text>
</TouchableHighlight>
</View>
)
}
}
個人理解,不一定正確
Flux 實現(xiàn)state的變化主要是根據(jù)
EventEmitter
進行通知改變鉴竭。所以使用的頁面都有
componentDidMount() {
AppStore.addChangeListener(this._onChange.bind(this));
}
componentWillUnmount() {
AppStore.removeChangeListener(this._onChange.bind(this));
}
_onChange() {
this.setState({
number: AppStore.getNumber()
});
}
三個方法歧譬。這三個方法實現(xiàn)并監(jiān)聽了number的變化,并改變state搏存,進行頁面的刷新瑰步。
初步理解