关于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是一个单页面应用,大多数的资源需要在首次渲染前就加载 ...
随机推荐
- Android实战:手把手实现“捧腹网”APP(一)-----捧腹网网页分析、数据获取
Android实战:手把手实现"捧腹网"APP(一)-–捧腹网网页分析.数据获取 Android实战:手把手实现"捧腹网"APP(二)-–捧腹APP原型设计.实 ...
- 读取服务器的windows共享。。
有些windows共享可以直接登录,有些需要帐号密码帐号密码 有些电脑在输入框输入ip没有弹出登录帐号密码的地方 如下方法可以登录 输入:net use \\ip\ipc$ /del 回车.(例如:& ...
- InteractiveHtmlBom 在手机上无法显示 BOM List 和装配图的问题
InteractiveHtmlBom 在手机上无法显示 BOM List 和装配图的问题 InteractiveHtmlBom 插件是一款用于 KiCad BOM 装配图生成插件. 最近新生成的 文件 ...
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛C. Sum【脑洞题】
限制:1000ms 32768K Define the function S(x) for xx is a positive integer. S(x) equals to the sum of al ...
- HDU 5673 Robot【卡特兰数】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题意: 有一个机器人位于坐标原点上.每秒钟机器人都可以向右移到一个单位距离,或者在原地不动.如 ...
- iOS之友盟错误统计解决
http://www.cocoachina.com/ios/20150720/12627.html http://lieyunye.github.io/blog/2013/09/10/how-to-a ...
- iOS 11 适配UIWebView,页面下移20的问题
方案1: AppDelegate文件 didFinishLaunchingWithOptions()中添加如下代码 if (@available(iOS 11.0, *)) { [[UIScrollV ...
- 从零学React Native之01创建第一个程序
本篇首发于简书 欢迎关注 上一篇文章是时候了解React Native了介绍了React Native.大家应该对React Native有个初步的认识. 接下来我们就可以初始化一个React Nat ...
- USDT钱包安装
安装USDT钱包 wget https://bintray.com/artifact/download/omni/OmniBinaries/omnicore-0.4.0-x86_64-linux-gn ...
- selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面(转)
selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面 博客分类: Selenium-webdriver 元素拖放drag and drop Q群里 ...