关于redux和react-redux使用combinereducers之后的问题
最近用react写项目的时候,开始复习之前学过的redux,记录一下一些坑,以防忘记
我现在的redux目录下有这么些东西
首先是index.js
import { createStore } from 'redux'
import player from './player_reducer'
let store = createStore(player)
export default store
然后是player.js
import { PLAY_LOCALMUSIC,CAN_CHANGE_PLAY_STATUS,CANNT_CHANGE_PLAY_STATUS,OPEN_PLAY_DETAIL,PLAY_STATUS } from './actionType'
const defaultState = {
playlocalIndex:0,
playtype:1,
canchangeplaystatus:false,
open_play_detail:false,
play_status:false,
playingdata:{}
}
export default function player(state = defaultState, action) {
//函数体
return state
}
然后是封装的actioncreater和actiontype
export const PLAY_LOCALMUSIC = 'play_localmusic'
export const CAN_CHANGE_PLAY_STATUS = 'can_change_play_status'
import { PLAY_LOCALMUSIC,CAN_CHANGE_PLAY_STATUS,CANNT_CHANGE_PLAY_STATUS,OPEN_PLAY_DETAIL,PLAY_STATUS } from "./actionType";
export const play_localmusic = (index,play_type) => ({
type:PLAY_LOCALMUSIC,
index:index,
play_type:play_type
})
export const canchangeplaystatus = () => ({
type:CAN_CHANGE_PLAY_STATUS,
})
现在一切正常,当更改store触发函数后打印此时的sotre.getState(),得到的结果是这样的
import { createStore } from 'redux'
import neteasecloudmusic from './app'
let store = createStore(neteasecloudmusic)
export default store

现在由于项目变复杂需要多个reducer共通管理store,则使用了combinereducers,更改如下,新建app.js
import { combineReducers } from 'redux';
import player from './player_reducer';
const neteasecloudmusic = combineReducers({
player
})
export default neteasecloudmusic;
修改index.js
import { createStore } from 'redux'
import neteasecloudmusic from './app'
let store = createStore(neteasecloudmusic)
export default store
player.js保持不动,由于我到这个时候没有使用connect,直接使用store.getState()进行的渲染,所以这时候关于redux的所有功能都会失效,原因在于这个时候再次打印store.getState()

这个时候的state变成了一个对象(之前也是一个对象),只不过现在被分割出来了
然后就是connect的用法,在组件页面引入connect
import { connect } from 'react-redux'
然后组件的export做一点改动
const mapstatetoprops = (state) => {
return{
canchangeplaystatus:state.player.canchangeplaystatus,
playtype:state.player.playtype,
playlocalIndex:state.player.playlocalIndex,
}
}
const mapdistoprops = (dispatch) => {
return{
open_play_detail (data) {
console.log('为什么啊')
const action = openplaydetail(true,data)
dispatch(action)
}
}
}
export default connect(mapstatetoprops,mapdistoprops)(Player)
通过connect,将store的数据以及dispatch方法绑定在了组件的props上,可以直接通过this.props访问到store里的数据,不用使用store.getState(),而且props会根据变化渲染页面
同时如果使用了中间件,比如thunk
import { createStore, applyMiddleware } from 'redux'
import neteasecloudmusic from './app'
import thunk from 'redux-thunk'
let store = createStore(neteasecloudmusic,applyMiddleware(thunk))
export default store
之前的action是这样(举个例子)
export const openplaydetail = (Bool,musicdata) => ({
type:OPEN_PLAY_DETAIL,
playdetail_status:Bool,
playing_data:musicdata
})
那么此时action就可以是一个函数,而不再仅仅是对象,此时的action可以这样写,例如
export const openplaydetail = (Bool,musicdata) => {
return (dispatch) => {
dispatch({
type:OPEN_PLAY_DETAIL,
playdetail_status:Bool,
playing_data:musicdata
})
}
}
在return里面可以执行一些操作,例如异步请求等等。而且这个时候可以不止进行一个dispatch,可以进行多个dispatch
export const openplaydetail = (Bool,musicdata) => {
return (dispatch) => {
dispatch({
type:OPEN_PLAY_DETAIL,
playdetail_status:Bool,
playing_data:musicdata
})
dispatch({
type:OPEN_USER_DETAIL,
open_user_detail:false
})
}
}
关于redux和react-redux使用combinereducers之后的问题的更多相关文章
- webpack+react+redux+es6开发模式
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- react+redux教程(八)连接数据库的redux程序
前面所有的教程都是解读官方的示例代码,是时候我们自己写个连接数据库的redux程序了! 例子 这个例子代码,是我自己写的程序,一个非常简单的todo,但是包含了redux插件的用法,中间件的用法,连接 ...
- react+redux教程(五)异步、单一state树结构、componentWillReceiveProps
今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...
- react+redux教程(四)undo、devtools、router
上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...
- react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware
今天,我们通过解读官方示例代码(counter)的方式来学习react+redux. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...
- React + Redux 入坑指南
Redux 原理 1. 单一数据源 all states ==>Store 随着组件的复杂度上升(包括交互逻辑和业务逻辑),数据来源逐渐混乱,导致组件内部数据调用十分复杂,会产生数据冗余或者混用 ...
- 【原】react+redux实战
摘要:因为最近搞懂了redux的异步操作,所以觉得可以用react+redux来做一个小小的项目了,以此来加深一下印象.切记,是小小的项目,所以项目肯定是比较简单的啦,哈哈. 项目效果图如图所示:(因 ...
- webpack+react+redux+es6
一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...
- react+redux+generation-modation脚手架添加一个todolist
当我遇到问题: 要沉着冷静. 要管理好时间. 别被bug或error搞的不高兴,要高兴,又有煅炼思维的机会了. 要思考这是为什么? 要搞清楚问题的本质. 要探究问题,探究数据的流动. TodoList ...
- 详解react/redux的服务端渲染:页面性能与SEO
亟待解决的疑问 为什么服务端渲染首屏渲染快?(对比客户端首屏渲染) react客户端渲染的一大痛点就是首屏渲染速度慢问题,因为react是一个单页面应用,大多数的资源需要在首次渲染前就加载 ...
随机推荐
- HTML之CSS标签
1.CSS选择器 1).id选择器 2).class选择器 3).标签选择器 4).层级选择器(空格) (1)id层级选择器 (2)class层级选择器 5).组合选择器(逗 ...
- 洛谷 P1829 [国家集训队]Crash的数字表格 / JZPTAB(莫比乌斯反演)
题意:求$\sum_{i=1}^{n}\sum_{j=1}^{m}lcm(i,j)$. 开始开心(自闭)化简: $\sum_{i=1}^{n}\sum_{j=1}^{m}lcm(i,j)$ =$\su ...
- .net 数据表格显示控件
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/chenjinge7/article/details/30470609 1. GridView 控件 ...
- oracle Wrap加密
wrap就是一个加密的工具 参数: Iname 输入文件的名称 Oname输出文件的名称 如何加密Oracle中的存储过程,从而在schema下看不到其源码? 软件环境: 1.操作系统:Windows ...
- lavarel box 地址
https://atlas.hashicorp.com/laravel/boxes/homestead download URL https://atlas.hashicorp.com/laravel ...
- part10.3-字符驱动访问揭秘
- 学习canvas画布
我们可以用画布(Canvas)绘制各种图形,下面代码是绘制的一个圆形: <!DOCTYPE html> <html> <head> <title>Can ...
- 微信公众号无法使用css3的多行省略
解决通过伪元素 .text{ width: 100%; position:relative; overflow:hidden; height: 20px /* overflow : hidden; t ...
- CODE FESTIVAL 2017 qual A C Palindromic Matrix(补题)
彩笔看到题目后,除了懵逼,没有啥反应了,唯一想的就是 这是不是dp啊?看了题解才发现,原来是这样啊. 画几个矩阵看看就能看出来规律. 思路:先假设这是个M * N的矩阵 如果M和N都是偶数,则每个出现 ...
- POJ 1961 Period 还是next数组的含义、
题意:求所给串的前缀(包括原串)中有多少循环串(子串长度至少要是周期的两倍) 思路:还是next数组的应用问题.如果不懂next数组的话 http://www.cnblogs.com/sasuke-/ ...