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的更多相关文章

  1. 前端(十一):props、state及redux关系梳理

    所谓状态机,是一种抽象的数据模型,是“事物发展的趋势”,其原理是事件驱动.广泛地讲,世界万物都是状态机. 一.状态机是一种抽象的数据模型 在react中,props和state都可以用来传递数据.这里 ...

  2. redux & multi dispatch & async await

    redux & multi dispatch & async await 同时发送多个 action, 怎么保证按序返回数据 dispatch multi actions http:/ ...

  3. 【React】360- 完全理解 redux(从零实现一个 redux)

    点击上方"前端自习课"关注,学习起来~ 前言 记得开始接触 react 技术栈的时候,最难理解的地方就是 redux.全是新名词:reducer.store.dispatch.mi ...

  4. react+redux教程(七)自定义redux中间件

    今天,我们要讲解的是自定义redux中间件这个知识点.本节内容非常抽象,特别是中间件的定义原理,那多层的函数嵌套和串联,需要极强逻辑思维能力才能完全消化吸收.不过我会多罗嗦几句,所以不用担心. 例子 ...

  5. 关于props和state以及redux中的state

    React的数据模型分为共有数据和私有数据,共有数据可以在组件间进行传递,私有数据为当前组件私有.共有数据在React中使用props对象来调用,它包含标签所有的属性名称和属性值,props对象有三个 ...

  6. [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 ...

  7. [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 ...

  8. React修改state(非redux)中数组和对象里边的某一个属性的值

    在使用React时,会经常需要处理state里边设置的初始值以达到我们的实际需求,比如从接口获取到列表数据后要赋值给定义的列表初始值,然后数据驱动view视图进而呈现在我们眼前,这种最简单的赋值方式实 ...

  9. redux源码解析(深度解析redux+异步demo)

    redux源码解析 1.首先让我们看看都有哪些内容 2.让我们看看redux的流程图 Store:一个库,保存数据的地方,整个项目只有一个 创建store Redux提供 creatStore 函数来 ...

随机推荐

  1. 【转】SDP file

    SDP file Introduction The Session Description Protocol (SDP) is a format for describing the initiali ...

  2. IOS 五星评分控件

    程序中需要打分的功能,在网上找了几个,都不是很满意.下面是实现出的效果.可以点击,可以拖动. 使用方法:初始化控件. TQStarRatingView *starRatingView = [[TQSt ...

  3. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  4. [Everyday Mathematics]20150114

    设 $a_0$, $d$ 给定, $a_k=a_0+kd$, $k=0,1,\cdots,n$. 试求如下 $n+1$ 阶行列式的值: $$\bex \sev{\ba{ccccc} a_0&a ...

  5. 最短路+线段交 POJ 1556 好题

    // 最短路+线段交 POJ 1556 好题 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终 ...

  6. Python之Tkinter模块学习

    本文转载自:http://www.cnblogs.com/kaituorensheng/p/3287652.html Tkinter模块("Tk 接口")是Python的标准Tk ...

  7. Javascript异步请求你能捕获到异常吗?

    Javascript异步请求你能捕获到异常吗? 异常处理是程序发布之前必须要解决的问题,不经过异常处理的应用会让用户对产品失去信心.在异常处理中,我们一贯的做法是按照函数调用的次序,将异常从数据访问层 ...

  8. linux appear packet loss solution

    故障排查: 早上突然收到nagios服务器check_icmp的报警,报警显示一台网站服务器的内网网络有问题.因为那台服务器挂载了内网的NFS,因此内网的网络就采用nagios的check_icmp来 ...

  9. homework-06

    围棋问题 关于代码的阅读,写注释,我的代码阅读量和阅读能力都有限,而且是关于没有基础的围棋问题,JAVA和C#混合的程序.不免参考了其他同学的思路,忘老师见谅. 1.playPrev(GoMove) ...

  10. CodeForces 489A SwapSort (选择排序法)

    SwapSort 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/A Description In this problem yo ...