[关闭]
@iwanglong 2018-11-08T06:47:17.000000Z 字数 3008 阅读 286

react-native 中使用redux

技术


对于刚学习rn的人来说,看到redux肯定会一脸懵。redux是干啥的呢,网上也有很多介绍,我就不做过多介绍了,个人把他理解成一个 小型数据库,用来处理数据的。更多详细内容看官方的吧,我只做一个个人对其理解的简单介绍

首先我们知道rn中通过state、props这两种方式控制组件,redux就是通过改变state来控制组件的变化。怎么改变的呢,大体讲一下流程吧:
1、首先我们点击按钮或者网络数据请求回来的时候需要发消息到action内
2、action将消息预处理,即区分type,然后返回给store。
3、store将分类好的消息分配到reducer中处理state。
4、reducer接收到消息、根据type做对应的处理,生成新的state返回给store
5、store控制页面渲染

不多说了,还是边上代码边做讲解比较好,先用着,然后慢慢再理解。当我们使用redux的时候就需要在项目里添加redux依赖。

  1. npm install --save redux
  2. npm install --save react-redux
  3. npm install --save redux-thunk

redux核心就三部分:Action、Reducer、Store。
* Action
想要更新state中的数据,就要发起一个动作,这就用到action了。Action内部必须使用一个字符串类型的type来表示要执行的动作。一般type都定义成字符串常量。Action只能描述有事情发生了,不能搞其他的。

  1. import * as types from '../constants/LoginTypes'
  2. let user = {
  3. name: '小明',
  4. age: 25
  5. }
  6. // 这里发起Action
  7. export function login() {
  8. return dispatch => {
  9. dispatch(isLogining());
  10. let result = fetch('https://www.baidu.com')
  11. .then((res) => {
  12. dispatch(loginSuccess(true, user))
  13. }).catch((err) => {
  14. dispatch(loginError(false))
  15. })
  16. }
  17. }
  18. // 这个Action标识正在登录
  19. function isLogining () {
  20. return {
  21. type: types.LOGIN_IN_DOING
  22. }
  23. }
  24. // 这个Action标识登录成功
  25. function loginSuccess(isSuccess, user) {
  26. console.log('loginSuccess')
  27. return {
  28. type: types.LOGIN_IN_DONE,
  29. user: user.name
  30. }
  31. }
  32. // 这个Action标识登录失败
  33. function loginError(isSuccess){
  34. console.log('error')
  35. return {
  36. type: types.LOGIN_IN_ERROR
  37. }
  38. }
  1. {
  2. status:0,
  3. userName:"小李",
  4. age:25,
  5. sex:"man"
  6. address:"北京"
  7. }

还是直接上代码吧

  1. import * as types from '../constants/LoginTypes'
  2. const initialState = {
  3. status: '点击登录',
  4. isSuccess: false,
  5. user: null
  6. }
  7. export default function longinIn(state = initialState, action) {
  8. switch (action.type) {
  9. case types.LOGIN_IN_DOING:
  10. return {
  11. ...state,
  12. status: '正在登录',
  13. isSuccess: false,
  14. user: null
  15. }
  16. break;
  17. case types.LOGIN_IN_DONE:
  18. return {
  19. ...state,
  20. status: '登录成功',
  21. isSuccess: true,
  22. user: action.user
  23. }
  24. break;
  25. case types.LOGIN_IN_ERROR:
  26. return {
  27. ...state,
  28. status: '登录失败',
  29. isSuccess: false,
  30. user: null
  31. }
  32. default:
  33. return state;
  34. }
  35. }
  36. // 当然我们会有多个reducer,那么我们需要把不同的reducer关联到一起提供给store
  37. import { combineReducers } from 'redux'
  38. import loginIn from './loginReducer'
  39. import counterIn from './CounterReducer'
  40. module.exports = combineReducers({
  41. loginIn:loginIn,
  42. counterIn:counterIn
  43. })

有码就好理解我说的model了吧,initialState是初始化。下边是根据action的type类型做相应的处理。
* Store
store是把action、reducer联系到一起的对象。它负责一下职责

  • 维持应用的 state;
  • 提供 getState() 方法获取 state;
  • 提供 dispatch(action) 方法更新 state;
  • 通过 subscribe(listener) 注册监听器;
  • 通过 subscribe(listener) 返回的函数注销监听器。
  1. 'use strict';
  2. import { createStore, applyMiddleware } from 'redux';
  3. import thunkMiddleware from 'redux-thunk';
  4. import rootReducer from '../reducers/index';
  5. const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore);
  6. export default function configureStore(initialState) {
  7. const store = createStoreWithMiddleware(rootReducer, initialState)
  8. return store;
  9. }

Redux 应用只有一个单一的store。当需要拆分数据处理逻辑时,你应该使用 reducer 组合 而不是创建多个 store。

  1. const mapStateToProps = (state) => {
  2. return {
  3. user: state.loginIn.user,
  4. }
  5. }
  6. const mapDispatchToProps = (dispatch) => {
  7. return {
  8. actions: bindActionCreators({
  9. ...loginAction
  10. }, dispatch)
  11. }
  12. }
  13. module.exports = connect(mapStateToProps, mapDispatchToProps, undefined, { withRef: true })(App)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注