前言

redux和react-redux的关系:
  redux就是一个存储数据的对象,并提供了获取/设置store中的属性的解决方案,react-redux是连接react和redux桥梁的封装。

使用

0.目录结构
  未标明[type=dir]就是文件,否则是文件夹

|__package.json
src
|____
_redux [type=dir]
components [type=dir]
|____
Header.js
Main.js
ThemeSwitch.js
store [type=dir]
App.js
index.js

1.由于版本问题有时候会引来大坑,所以贴一下版本号
  package.json

{
"name": "mydemo3",
"version": "0.1.0",
"private": true,
"dependencies": {
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-redux": "^5.0.7",
"react-scripts": "1.1.4",
"redux": "^4.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker'; import { Provider } from 'react-redux' //从react-redux中取出包裹层组件
import store from './store/store' //取出初始化的store文件 ReactDOM.render(
<Provider store={store}>
<App />
</Provider>, document.getElementById('root')); registerServiceWorker();

App.js

import React, { Component } from 'react'
import Header from './components/Header'
import Content from './components/Main'
import './index.css' class App extends Component {
render () {
return (
<div>
<Header />
<Content />
</div>
)
}
} export default App

reducer.js

export const themeReducer = (state, action) => {
if (!state) return {
themeColor: 'red'
}
switch (action.type) {
case 'CHANGE_COLOR':
return { ...state, themeColor: action.themeColor }
default:
return state
}
}

store.js

import { createStore } from 'redux'
import { themeReducer } from './reducer' const store = createStore(themeReducer) export default store

Header.js

import React, { Component } from 'react'
import { connect } from 'react-redux' class Header extends Component {
render () {
return (
<h1 style={{ color: this.props.themeColor }}>this is header</h1>
)
}
} const mapStateToProps = (state) => {
return {
themeColor: state.themeColor
}
} Header = connect(mapStateToProps)(Header) export default Header

Main.js

import React, { Component } from 'react'
import ThemeSwitch from './ThemeSwitch'
import { connect } from 'react-redux' class Content extends Component {
constructor () {
super()
this.state = { themeColor: '' }
} render () {
return (
<div>
<p style={{ color: this.props.themeColor }}>this is content</p>
<ThemeSwitch />
</div>
)
}
} const mapStateToProps = state => {
return {
themeColor: state.themeColor
}
} Content = connect(mapStateToProps)(Content) export default Content

ThemeSwitch .js

import React, { Component } from 'react'
import { connect } from 'react-redux' class ThemeSwitch extends Component {
constructor () {
super()
this.state = { themeColor: '' }
} // dispatch action 去改变颜色
handleSwitchColor = (color) => {
if (this.props.onSwitchColor) {
this.props.onSwitchColor(color)
}
} render () {
return (
<div>
<button
style={{ color: this.props.themeColor }}
onClick={()=>{ this.handleSwitchColor('green') }}>Red</button>
<button
style={{ color: this.props.themeColor }}
onClick={()=>{ this.handleSwitchColor('blue') }}>Blue</button>
</div>
)
}
} const mapStateToProps = state => {
return {
themeColor: state.themeColor
}
} const mapDispatchToProps = (dispatch) => {
return {
onSwitchColor: (color) => {
dispatch({ type: 'CHANGE_COLOR', themeColor: color })
}
}
} ThemeSwitch = connect(mapStateToProps, mapDispatchToProps)(ThemeSwitch) export default ThemeSwitch

主要的几点:
1、index.js中:
  import { Provider } from 'react-redux'这里要从redux中引入,而不是redux中引入。
  <Provider store={store}><App /> </Provider>'这里要把store传入组件Provider,然后必须把App组件放入其中。

2、store/store.js中:
  主要是为了初始化redux。

3、connect
  是将:store的指定属性、方法注入到组件的this.props中去。

实例地址:
https://github.com/wenwenwei/learn-project/tree/master/react-redux

想要更深入理解reudx可以参考文章:
https://segmentfault.com/a/1190000012976767#articleHeader14

react学习之redux和redux-react用法的更多相关文章

  1. React学习笔记-1-什么是react,react环境搭建以及第一个react实例

    什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...

  2. React学习资料

    以下是我整理的React学习资料,包括:React基础.Redux.reat-router, redux middleware, higher order components, React验证等, ...

  3. React学习笔记(一) 基础知识

    现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我. React的基 ...

  4. (转)2019年 React 新手学习指南 – 从 React 学习线路图说开去

    原文:https://www.html.cn/archives/10111 注:本文根据 React 开发者学习线路图(2018) 结构编写了很多新手如何学习 React 的建议.2019 年有标题党 ...

  5. AntDesign(React)学习-1 创建环境

    目录: AntDesign(React)学习-15 组件定义.connect.interface AntDesign(React)学习-14 使用UMI提供的antd模板 AntDesign(Reac ...

  6. React系列(一):React入门

    React简介 1.由来 React是有Facebook开发出来用于构建前端界面的JS组件库,由于其背后的强大背景,使得这款库在技术开发上完全没有问题. 2.React的优势 解决大规模项目开发中数据 ...

  7. React学习之redux

    在阅读本文之前,希望大家对以下知识点能提前有所了解并且上好厕所(文章有点长): 状态提升的概念 react高阶组件(函数) es6基础 pure 组件(纯函数) Dumb 组件 React.js的co ...

  8. react系列(四)Redux基本概念和使用

    Redux基本概念和使用 先从Flux开始 先放一个Flux官网的链接.需要fq. Flux是Facebook提出的一种构建客户端网页应用的应用架构,它是一种抽象程度很高的设计模式,鼓励单向数据流. ...

  9. React:快速上手(5)——掌握Redux(2)

    React:快速上手(5)——掌握Redux(2) 本文部分内容参考阮一峰的Redux教程. React-Redux原理 React-Redux运行机制 我觉得这张图清楚地描述React-Redux的 ...

  10. React:快速上手(4)——掌握Redux(1)

    React:快速上手(4)——掌握Redux 引入Redux 混乱的state管理 随着 JavaScript 单页应用开发日趋复杂,JavaScript 需要管理比任何时候都要多的 state (状 ...

随机推荐

  1. 我的Android进阶之旅------>使用ThumbnailUtils类获取视频的缩略图

    今天看了一段代码,是关于获取视频的缩略图的,让我认识了一个ThumbnailUtils类,代码如下. Bitmap bitmap = ThumbnailUtils.createVideoThumbna ...

  2. SpringBoot学习笔记(10):使用MongoDB来访问数据

    SpringBoot学习笔记(10):使用MongoDB来访问数据 快速开始 本指南将引导您完成使用Spring Data MongoDB构建应用程序的过程,该应用程序将数据存储在MongoDB(基于 ...

  3. 近200篇机器学习&深度学习资料分享【转载】

    编者按:本文收集了百来篇关于机器学习和深度学习的资料,含各种文档,视频,源码等.而且原文也会不定期的更新,望看到文章的朋友能够学到更多. <Brief History of Machine Le ...

  4. ubuntu 网络配置及ssh文件传输

    一.ubuntu网路配置 参考http://www.cnblogs.com/rusty/archive/2011/04/06/2007139.html /etc/network/interfaces ...

  5. POJ2443 Set Operation —— bitset

    题目链接:https://vjudge.net/problem/POJ-2443 Set Operation Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  6. HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...

  7. ES索引瘦身 禁用_source后需要设置field store才能获取数据 否则无法显示搜索结果

    在默认情况下,开启_all和_source 这样索引下来,占用空间很大. 根据我们单位的情况,我觉得可以将需要的字段保存在_all中,然后使用IK分词以备查询,其余的字段,则不存储. 并且禁用_sou ...

  8. 使用IE11的F12开发人员工具进行网页前端性能测试

    用IE访问被测网站(我的是IE11,EDGE浏览器相同),定位到你要测试的动作所在页面或被测页面的前一页.按F12调出开发人员工具,其它的功能我就不介绍了,直接切换到性能选项卡. 根据提示按快捷键ct ...

  9. 常用SASS封装

    结合Compass库和工作总结,列出了项目中最为常用的SASS片段.内容收集于网络,我进行了简单整理并测试正常,可以根据实际项目情况进行取舍,值得学习或直接应用,感谢! //重置浏览器默认样式@imp ...

  10. Spring 配置 详细

    一.连接池概述 数据库连接池概述: 数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指 ...