react之路:使用redux-immutable

github仓库地址:https://github.com/wanghao12345/react-book

简介

我们在使用redux中的combineReducers用来分离reducer后,如果组件去访问数据时,需要访问多一层属性,为了将访问这一层属性的方式变成使用函数进行访问。这里就可以使用redux-immutable中的combineReducers。

使用

1.引入redux-immutable中的combineReducers

将reducer.js中

import { combineReducers } from 'redux'

变成

import { combineReducers } from 'redux-immutable'

2.使用

 1 /**
 2  * 将仓库的state映射到props(获取state)
 3  * @param state
 4  */
 5 const mapStateToProps = (state) => {
 6     return {
 7         // 没有使用immutable
 8         // focused: state.header.focused
 9         // 使用了immutable
10         // focused: state.header.get('focused')
11         // 使用了redux-immutable(两种写法均可)
12         // focused: state.get('header').get('focused')
13         focused: state.getIn(['header', 'focused'])
14     }
15 }