网上别人的文档都是 直接 就是上redux
redux-thunk
react-redux
,immutable
这样的一套,这个有经验的看还行,新手看就很吃力了,须要了解一步一步的安装redux达到开发要求react
我以为这须要一个学习的过程,适得其反不是好事git
这是我写项目的逐步搭建的过程,欢迎查看源代码github-pinduoduogithub
由于
redux
是js的部分 因此不须要linknpm
npm install redux--save
安装完成后确承认以正常启动json
个人项目结构redux
和React项目同样的项目结构react-native
index.jsbash
import { createStore } from 'redux' import reducer from './reducer' export default createStore(reducer) // 导入state
reducer.jsapp
import actionTypes from './actionTypes' const defaultState = { // 初始化state data: 'my is redux!!!!' } export default (state = defaultState, action) => { console.log(action) if (action.type == actionTypes.CHANGE) { // 修改state const newState = JSON.parse(JSON.stringify(state)) newState.data = 'change data!!!' return newState } return state }
actionTypes.jside
export default { CHANGE: 'change' // 定义统一的type }
actionCreators.js
import actionTypes from './actionTypes' export function change() { // 统一管理action return { type: actionTypes.CHANGE } }
最后在页面里面
import React, { Component } from 'react' import { Text, StyleSheet, View, StatusBar, Dimensions, Button } from 'react-native' import store from '../../../store/index' // 导入store import { change } from '../../../store/actionCreators' // 导入action export default class Popular extends Component { constructor(props) { super(props) this.state = store.getState() // 初始化state,获取redux里面数据 store.subscribe(() => { // 订阅state的改变 this.setState(store.getState()) }) } render() { return ( <View> <Text>{this.state.data}</Text> <Button title="更新state" onPress={() => { store.dispatch(change()) }} /> <Button title="查看state" onPress={() => { console.log(store.getState()) }} /> </View> ) } } const styles = StyleSheet.create({})
最基础的redux
就使用成功了,可是这个还达不到咱们的开发要求,下面将引入react-redux
若是不了解
React-redux
,请学习后再说,否则确定看不懂,React-redux文档
以前咱们在组件里面使用Redux直接去获取数据,加入每一个页面都这样写,会很麻烦,因此咱们要借助react-redux
来帮咱们处理store
修改以前写的页面代码,去掉以前页面使用state
的地方
import React, { Component } from 'react' import { Text, StyleSheet, View, StatusBar, Dimensions, Button } from 'react-native' import { change } from '../../../store/actionCreators' class Popular extends Component { render() { return ( <View> <Text>{this.props.data}</Text> <Button title="更新state" onPress={() => { //.. }} /> <Button title="获取state" onPress={() => { //.. }} /> </View> ) } } const styles = StyleSheet.create({}) export default Popular
修改程序的挂载入口
index.js
/** @format */ import React, { Component } from 'react' import { Provider } from 'react-redux' import { AppRegistry } from 'react-native' import store from './app/store' import App from './app/routes/App' import { name } from './app.json' class Apps extends Component { render() { return ( // 挂载store,让app内部全部组件均可以使用 <Provider store={store}> <App /> </Provider> ) } } AppRegistry.registerComponent(name, () => Apps)
这里咱们就能够在组件里面经过connent
来接收了
import React, { Component } from 'react' import { connect } from 'react-redux' import { Text, StyleSheet, View, StatusBar, Button } from 'react-native' import { change } from '../../../store/actionCreators' class Popular extends Component { render() { return ( <View> <StatusBar translucent={true} // 设置沉浸式状态栏 正常状况下 状态栏高度为20 这里的20 须要页面元素距离最上面 paddingTop:20 backgroundColor={'rgba(0,0,0,0.1)'} // 设置状态栏颜色 animated={true} // 容许动画切换效果 /> <Text>{this.props.data}</Text> <Button title="更新state" onPress={this.props.changeData} /> <Button title="获取state" onPress={() => { console.log(this.props.data) }} /> </View> ) } } const styles = StyleSheet.create({}) const mapState = state => ({ data: state.data }) const mapDispatch = dispatch => ({ changeData() { dispatch(change()) } }) export default connect( mapState, mapDispatch )(Popular)
这里咱们React-redux
再次获取并修改了redux里面的数据,相对之下,使用React-redux
后,页面逻辑更加清楚
immutable在平常开发里面很常见,让咱们的数据更加严谨
很简单,首先安装
npm install immutable
处理咱们store的数据
import actionTypes from './actionTypes' import {fromJS} from 'immutable' const defaultState = fromJS({ // 将对象转成immutable对象 data: 'my is redux!!!!' }) export default (state = defaultState, action) => { if (action.type == actionTypes.CHANGE) { return state.set('data','change Redux!!!') } return state }
而后处理咱们页面里面引用数据的地方
const mapState = state => ({ data: state.get('data') // immutable对象使用get获取 })
将大量的store数据放在一块儿是很是很差的行为,咱们要将每一个组件之间的store
尽量的分离
这里用的是redux给咱们提供的 combineReducers 将store进行合并
修改页面目录结构,在页面目录里面建立store
组件内部的sore代码
Popular/store/reducer
import actionTypes from './actionTypes' import {fromJS} from 'immutable' const defaultState = fromJS({ data: 'my is redux!!!!' }) export default (state = defaultState, action) => { if (action.type == actionTypes.CHANGE) { return state.set('data','change Redux!!!') } return state }
Popular/store/actionTypes
export default { CHANGE: 'change' }
Popular/store/actionCreators
import actionTypes from './actionTypes' export function change() { return { type: actionTypes.CHANGE } }
Popular/store/index
import reducer from './reducer' import actionCreators from './actionCreators' import actionTypes from './actionTypes' export { reducer, actionCreators, actionTypes } // 使用入口
这样咱们就在组件内部新建了一个store,接下来咱们要把组件内部的store合并store里面
./store/reducer
import { combineReducers } from 'redux' import { reducer as homePopular } from '../src/home/Popular/store/index' export default combineReducers ({ homePopular: homePopular })
这就完成了store的合并,这里store变了,天然访问就变了
Popular.js
const mapState = state => ({ data: state.homePopular.get('data') })
redux
中间件我通常状况下使用
redux-thunk
npm install redux-thunk
import { createStore,applyMiddleware } from 'redux' import thunk from 'redux-thunk' import reducer from './reducer' export default createStore( reducer, applyMiddleware(thunk) )
这里不作样式了,会的人天然会,不会的学习一下,学会使用很简单