redux在react-native上使用(一)--加入redux
原始项目
这是非常简单的一个项目,就是一个计数器,只有两个文件
import React,{ Component } from 'react'; import { AppRegistry,StyleSheet,Text,View,TouchableOpacity } from 'react-native'; class Main extends Component { constructor(props) { super(props); this.state = { count: 5 } } _onPressReset() { this.setState({ count: 0 }) } _onPressInc() { this.setState({ count: this.state.count+1 }); } _onPressDec() { this.setState({ count: this.state.count-1 }); } render() { return ( <View style={styles.container}> <Text style={styles.counter}>{this.state.count}</Text> <TouchableOpacity style={styles.reset} onPress={()=>this._onPressReset()}> <Text>归零</Text> </TouchableOpacity> <TouchableOpacity style={styles.start} onPress={()=>this._onPressInc()}> <Text>加1</Text> </TouchableOpacity> <TouchableOpacity style={styles.stop} onPress={()=>this._onPressDec()}> <Text>减1</Text> </TouchableOpacity> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1,alignItems: 'center',justifyContent: 'center',flexDirection: 'column' },counter: { fontSize: 50,marginBottom: 70 },reset: { margin: 10,backgroundColor: 'yellow' },start: { margin: 10,stop: { margin: 10,backgroundColor: 'yellow' } }) AppRegistry.registerComponent('Helloworld',() => Main); 添加redux先添加 "dependencies": { ... "react-redux": "^4.4.5","redux": "^3.5.2","redux-logger": "^2.6.1" }, 再创建 export const INCREASE = 'INCREASE'; export const DECREASE = 'DECREASE'; export const RESET = 'RESET'; 创建 import { INCREASE,DECREASE,RESET } from './actionsTypes'; const increase = () => ({ type: INCREASE }); const decrease = () => ({ type: DECREASE }); const reset = () => ({ type: RESET }); export { increase,decrease,reset } 创建 import { combineReducers } from 'redux'; import { INCREASE,RESET} from './actionsTypes'; // 原始默认state const defaultState = { count: 5,factor: 1 } function counter(state = defaultState,action) { switch (action.type) { case INCREASE: return { ...state,count: state.count + state.factor }; case DECREASE: return { ...state,count: state.count - state.factor }; case RESET: return { ...state,count: 0 }; default: return state; } } export default combineReducers({ counter }); 创建 import { createStore,applyMiddleware,compose } from 'redux'; import createLogger from 'redux-logger'; import rootReducer from './reducers'; const configureStore = preloadedState => { return createStore ( rootReducer,preloadedState,compose ( applyMiddleware(createLogger()) ) ); } const store = configureStore(); export default store; 至此 index.ios.js更改: import { AppRegistry } from 'react-native'; import App from './app'; AppRegistry.registerComponent('Helloworld',() => App); app.js import React,{ Component } from 'righteact'; import { Provider } from 'react-redux'; import Home from './home'; import store from './store'; export default class App extends Component { render() { return ( <Provider store={store}> <Home/> </Provider> ); } } home.js在原来 import React,{ Component } from 'react'; import { StyleSheet,TouchableOpacity } from 'react-native'; import { connect } from 'react-redux'; import { increase,reset } from './actions'; class Home extends Component { _onPressReset() { this.props.dispatch(reset()); } _onPressInc() { this.props.dispatch(increase()); } _onPressDec() { this.props.dispatch(decrease()); } render() { return ( <View style={styles.container}> <Text style={styles.counter}>{this.props.counter.count}</Text> <TouchableOpacity style={styles.reset} onPress={()=>this._onPressReset()}> <Text>归零</Text> </TouchableOpacity> <TouchableOpacity style={styles.start} onPress={()=>this._onPressInc()}> <Text>加1</Text> </TouchableOpacity> <TouchableOpacity style={styles.stop} onPress={()=>this._onPressDec()}> <Text>减1</Text> </TouchableOpacity> </View> ); } } const styles = StyleSheet.create({ ... }) const mapStateToProps = state => ({ counter: state.counter }) export default connect(mapStateToProps)(Home); OK,大功告成,
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |