最近用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. oracle-ORA-01650错误

    Unable to extend rollback segment 原因:没有足够的撤销空间用来处理所有活动事务

  2. Eclipse中提示 找不到类 javax.servlet.http.HttpServletResponse

    问题如题, 解决方案如下: 复制tomcat的安装路径下\lib\servlet-api.jar 到WEB-INF/lib下即可.

  3. hdu4310 贪心

    考虑每次血口的要少 就按照一滴血多少伤害来计算.由于直接相除有小数.考虑x/y > a/b  =>  x*b >y*a; #include<stdio.h> #inclu ...

  4. jq 获取内容

    1.html()-设置或者返回被选元素的内容(包括html标记) $(selector).html() 2.text()-设置或者返回被选元素的内容 $(selector).text() 3.val( ...

  5. nginx设置301永久重定向

    https://blog.csdn.net/wzqzhq/article/details/53376501 比如说我的域名有多个,一个主域名www.zq110.com,多个次域名:www.aaa.co ...

  6. jQuery 鼠标移入图片 显示大图并跟随鼠标移动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. nodejs启本地服务器

    https.js var PORT = 8666;// var http = require('http'); var url=require('url'); var fs=require('fs') ...

  8. Spring Security入门篇——标签sec:authorize的使用

    Security框架可以精确控制页面的一个按钮.链接,它在页面上权限的控制实际上是通过它提供的标签来做到的 Security共有三类标签authorize authentication accessc ...

  9. Python 变量赋值

  10. PHPExcel 设置表格边框

    //设置单元格边框 $style_array = array( 'borders' => array( 'allborders' => array( 'style' => \PHPE ...