最近用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之后的问题的更多相关文章

  1. webpack+react+redux+es6开发模式

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  2. react+redux教程(八)连接数据库的redux程序

    前面所有的教程都是解读官方的示例代码,是时候我们自己写个连接数据库的redux程序了! 例子 这个例子代码,是我自己写的程序,一个非常简单的todo,但是包含了redux插件的用法,中间件的用法,连接 ...

  3. react+redux教程(五)异步、单一state树结构、componentWillReceiveProps

    今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...

  4. react+redux教程(四)undo、devtools、router

    上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...

  5. react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware

    今天,我们通过解读官方示例代码(counter)的方式来学习react+redux. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...

  6. React + Redux 入坑指南

    Redux 原理 1. 单一数据源 all states ==>Store 随着组件的复杂度上升(包括交互逻辑和业务逻辑),数据来源逐渐混乱,导致组件内部数据调用十分复杂,会产生数据冗余或者混用 ...

  7. 【原】react+redux实战

    摘要:因为最近搞懂了redux的异步操作,所以觉得可以用react+redux来做一个小小的项目了,以此来加深一下印象.切记,是小小的项目,所以项目肯定是比较简单的啦,哈哈. 项目效果图如图所示:(因 ...

  8. webpack+react+redux+es6

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  9. react+redux+generation-modation脚手架添加一个todolist

    当我遇到问题: 要沉着冷静. 要管理好时间. 别被bug或error搞的不高兴,要高兴,又有煅炼思维的机会了. 要思考这是为什么? 要搞清楚问题的本质. 要探究问题,探究数据的流动. TodoList ...

  10. 详解react/redux的服务端渲染:页面性能与SEO

        亟待解决的疑问 为什么服务端渲染首屏渲染快?(对比客户端首屏渲染)   react客户端渲染的一大痛点就是首屏渲染速度慢问题,因为react是一个单页面应用,大多数的资源需要在首次渲染前就加载 ...

随机推荐

  1. 尖峰7月线上技术分享--Hadoop、MySQL

      7月2号晚20:30-22:30 东大博士Dasight分享主题<大数据与Hadoop漫谈> 7月5号晚20:30-22:30  原支付宝MySQL首席DBA分享主题<MySQL ...

  2. TIJ——Chapter Nine:Interfaces

    A class containing abstract methods is called an abstract class. If a class Contains one of more abs ...

  3. LeetCode103 Binary Tree Zigzag Level Order Traversal

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  4. QT 开发ros gui过程中遇到:error: catkin_package() include dir 'include' does not exist relative to '/home/jun/catkin_ws/src/qt_ros_test' /opt/ros/kinetic/share/catkin/cmake/catkin_package.cmake:102 (_catkin_p

    这是因为在ros工作空间的包中没有include文件夹造成的,所以在该路径下创建include的文件夹,问题就解决了.

  5. 自定义View系列教程02--onMeasure源码详尽分析

    深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...

  6. Kafka数据迁移MaxCompute最佳实践

    摘要: 本文向您详细介绍如何使用DataWorks数据同步功能,将Kafka集群上的数据迁移到阿里云MaxCompute大数据计算服务. 前提条件 搭建Kafka集群 进行数据迁移前,您需要保证自己的 ...

  7. K8s中Pod健康检查源代码分析

    了解k8s中的Liveness和Readiness Liveness: 表明是否容器正在运行.如果liveness探测为fail,则kubelet会kill掉容器,并且会触发restart设置的策略. ...

  8. 用JavaScript判断网站是在手机端还是在PC端打开的方法

    我们可以在网站的首页加上一段JavaScript代码对用户的浏览器进行判断,从而显示不同的网址,代码如下: <script type="text/javascript"> ...

  9. 重置Mysql自增列的开始序号

    ALTER TABLE  TableName AUTO_INCREMENT = 5; 代表重新从5开始(包括5)

  10. Samba服务器 安装

    yum -y install samba cp /etc/samba/smb.conf /etc/samba/smb.conf.bak cat >> /etc/samba/smb.conf ...