react @connect

import React from 'react';
import { connect } from 'react-redux'
import {add_A,rem_R,addAsync} from './index.redux.js'


/**没使用@conncet注解方式之前的实现
 * 接收store赋值给组件内部的props
 * const mapStatetoProps = (state) =>{
 *     return {num:state}
 *}
 *将store当中的所有的actionCreator放入actionCreators
 *const actionCreators = {add_A,rem_R,addAsync}
 *将App传入connect当中 将所有的函数和参数都给到App 生成一个新的App 组件内部通过this.props即可获取 传入的参数和方法
 *App = connect(mapStatetoProps,actionCreators)(App)
 */



//使用注解的方式修改state和组件之间的传值
@connect(
    //你需要state当中的什么参数 取出来就会放到props相对的参数当中
    state=>({num:state}),
    //你需要state当中的什么方法就可以写到下面的大括号中就能被放到props当中 并且会自动dispatch
    {add_A,rem_R,addAsync}
)
 class App extends React.Component{
    constructor(props){
        super()
    }
    render(){
        return (
            <div>
                <h1>现在是数字几{this.props.num}</h1>
                {/*使用了connect之后这里不需要在手动的dispatch了直接调用即可*/}
                <button onClick={this.props.add_A}>加</button>
                <button onClick={this.props.rem_R}>减</button>
                {/*这里点击完成之后就会触发异步方法 在两秒之后更新*/}
                <button onClick={this.props.addAsync}>等两秒再加</button>
             </div>
        )
    }
}

export default App
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值