[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>
        );
    }
}
View Code
复制代码

 

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.

posted @   Zhentiw  阅读(299)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-08-25 [Angular 2] 8. Better ES5 Code
点击右上角即可分享
微信分享提示