[Redux] Accessing Dispatch and State with Redux -- connect
If you have props and actions, you want one component to access those props and actions, one solution is pass those from parent to this component. But one problem for this solution is if the there are many nested component between, you need to pass the props and actions all the way down to that component. It would be quite plainful and hard to maintain.
One better solution is using "connect" which provided by 'react-redux' libaray.
<Provider store={store}>
<Router history={history}>
<Route path="/" component={Main}>
<IndexRoute component={PhotoGrid}></IndexRoute>
<Route path="/view/:postId" component={SingleGrid}></Route>
</Route>
</Router>
</Provider>
Let's say I want to pass the props and actions to 'Main' component.
And the Main component it looks like:
import React from 'react';
import { Link } from 'react-router'; export default class Main extends React.Component{
constructor(){
super();
}
render(){
return (
<div>
<h1>
<Link to="/"> Reduxuxstagram </Link>
</h1>
/**
* To enable pass this.props to the children
* We need to use React.cloneElement()
* */
{React.cloneElement(this.props.children, this.props)}
</div>
);
}
}
What we can do is create a new file called 'App.js' to connect 'Main' component:
//App.js
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import * as actionCreators from '../actionCreator';
import Main from './Main';
function mapStateToProps(state){
return {
posts: state.posts,
commnets: state.comments
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators(actionCreators, dispatch);
}
// The idea to pass props and actions to Main Component is to use 'connect'.
const App = connect(
mapStateToProps,
mapDispatchToProps
)(Main);
export default App;
Now instead using 'Main' component in the Route, we use 'App' istead:
<Provider store={store}>
<Router history={history}>
<Route path="/" component={App}>
<IndexRoute component={PhotoGrid}></IndexRoute>
<Route path="/view/:postId" component={SingleGrid}></Route>
</Route>
</Router>
</Provider>
So if we open React devtool, and check the Main component, we can see, 'posts' and 'comments' from state are available, also the actions 'addComment', 'removeComment' also available to Main component.

[Redux] Accessing Dispatch and State with Redux -- connect的更多相关文章
- 前端(十一):props、state及redux关系梳理
所谓状态机,是一种抽象的数据模型,是“事物发展的趋势”,其原理是事件驱动.广泛地讲,世界万物都是状态机. 一.状态机是一种抽象的数据模型 在react中,props和state都可以用来传递数据.这里 ...
- redux & multi dispatch & async await
redux & multi dispatch & async await 同时发送多个 action, 怎么保证按序返回数据 dispatch multi actions http:/ ...
- 【React】360- 完全理解 redux(从零实现一个 redux)
点击上方"前端自习课"关注,学习起来~ 前言 记得开始接触 react 技术栈的时候,最难理解的地方就是 redux.全是新名词:reducer.store.dispatch.mi ...
- react+redux教程(七)自定义redux中间件
今天,我们要讲解的是自定义redux中间件这个知识点.本节内容非常抽象,特别是中间件的定义原理,那多层的函数嵌套和串联,需要极强逻辑思维能力才能完全消化吸收.不过我会多罗嗦几句,所以不用担心. 例子 ...
- 关于props和state以及redux中的state
React的数据模型分为共有数据和私有数据,共有数据可以在组件间进行传递,私有数据为当前组件私有.共有数据在React中使用props对象来调用,它包含标签所有的属性名称和属性值,props对象有三个 ...
- [Redux] Wrapping dispatch() to Log Actions
We will learn how centralized updates in Redux let us log every state change to the console along wi ...
- [Redux] Supplying the Initial State
We will learn how to start a Redux app with a previously persisted state, and how it merges with the ...
- React修改state(非redux)中数组和对象里边的某一个属性的值
在使用React时,会经常需要处理state里边设置的初始值以达到我们的实际需求,比如从接口获取到列表数据后要赋值给定义的列表初始值,然后数据驱动view视图进而呈现在我们眼前,这种最简单的赋值方式实 ...
- redux源码解析(深度解析redux+异步demo)
redux源码解析 1.首先让我们看看都有哪些内容 2.让我们看看redux的流程图 Store:一个库,保存数据的地方,整个项目只有一个 创建store Redux提供 creatStore 函数来 ...
随机推荐
- oracle返回多结果集
kavy 原文 oracle返回多结果集 Oracle存储过程: create or replace procedure P_Sel_TopCount2(in_top in number, out_c ...
- Oracle 数据乱码
原文 Oracle 数据乱码 服务器配置环境变量 NLS_LANG:American_america.ZHS16GBK
- 常用SQL语句汇总整理
1.SQL 插入语句得到自动生成的递增ID 值 insert into Table1(Name,des,num) values (''ltp'',''thisisbest'',10); select ...
- 2.2……测试
概述 黑盒测试: 是以用户的角度,从输入数据与输出数据的对应关系出发进行测试的. 白盒测试: 又称结构测试.透明盒测试.逻辑驱动测试或基于代码的测试. 单元测试: 又称模块测试,是开发者编写的 ...
- Ext列表展现--普通排序sortable--全局排序remoteSort(EXTJS 全局排序问题)
关于Ext的排序问题,一般涉及到两种方式. A.一种是默认的客户端排序机制,对当前页进行排序.sortable 这种排序模式不用多说,是人都会: 1.可以在Ext.grid.ColumnModel列模 ...
- JAVA与多线程开发(线程基础、继承Thread类来定义自己的线程、实现Runnable接口来解决单继承局限性、控制多线程程并发)
实现线程并发有两种方式:1)继承Thread类:2)实现Runnable接口. 线程基础 1)程序.进程.线程:并行.并发. 2)线程生命周期:创建状态(new一个线程对象).就绪状态(调用该对象的s ...
- UIButton 在 iOS7.0与iOS7.1 中关于enabled的一点区别
前些日子,在一台iOS7.0的设备上进行调试,关于UIButton的一部分代码如下 1 self.btn_loadmore.enabled = NO; 2 [self.btn_loadmore set ...
- Python xlsx 读取
代码示例 #!/usr/bin/env python import xlrd, sys, re, os workbook = xlrd.open_workbook( sys.argv[1] )for ...
- hibernate 打印sql和参数的配置
1.配置spring-hiberbate.xml:<prop key="hibernate.show_sql">true</prop>--强制打印sql 不 ...
- SpringMVC学习笔记
1.严格实现MVC设计思想的框架,严格分层,减少耦合: 2.组件(红色必需) 2.1 DispatcherServlet 前端控制器 2.2 Controller 业务控制器 2.3 Handler ...