react 父组件传递函数给子组件

文章参考

问题来源

学习react过程中向把Lits列表中的Item项抽象成一个组件,子组件内部有个SwipeAction组件,它需要接受一个对象,对象中包含一个自定义函数,不是直接通过props传递的函数;这就有点晕了,不知道jsx能否找到函数的所属对象,特此做了相关记录

案例

父组件

// 通过高阶函数返回定义的事件,高阶函数获取变量参数,在返回函数中获取事件对象
deleteGoodsByIdByPost(goodsId, index){
    let that = this;
    that.state.goodsList.splice(index, 1);
    that.setState({
        goodsList : that.state.goodsList
    })
    GoodsService.deleteGoodsByIdByPost(goodsId)
        .then(function(res){
        if(res.data == "sucess"){
            Toast.success('删除成功 !!!', 1);
        }
    });
}

render () {
	    // 顶部右侧按钮,实现页面切换
	    let typeInBtn = <Link to={ "/goodsManage/goodsDetailsAdd" }>录入</Link>;
		return (
            <div>
                <div className={'contentStyle'}>
                    {this.state.goodsList.map((currentObj, index, originArr) => {
                        let rightSwipeConfigArray = [{
                            text: '删除',
                            onPress: this.deleteGoodsByIdByPost.bind(this,currentObj.goodsId, index),
                            style: { backgroundColor: '#F4333C', color: 'white' },
                        }];
                        let url = "/goodsManage/goodsDetailsUpdate/"+currentObj.goodsId;
                        return (
                            <GoodsListItemComp key={index} url={url} goodsObj={currentObj} rightSwipeConfigArray={rightSwipeConfigArray}></GoodsListItemComp>
                        );
                    })}
                </div>
            </div>
		);
	}

子组件GoodsListItemComp

import BaseComponent from '../core/BaseComponent.js';
import { SwipeAction, Button, Toast } from 'antd-mobile';
import { Link } from 'react-router-dom';

/**
 * 商品列表的 Item 项组件
 */
class GoodsListItemComp extends BaseComponent {

    // GoodsDetails的构造函数,可以执行初始化的操作
    constructor (props) {
        super(props);
        let that = this;
        this.imageStyle = {
            width: "90px",
            height: "auto",
            float: "left"
        };
        let  propsRightSwipeConfigArray = null;
        if(props.rightSwipeConfigArray == undefined){
            propsRightSwipeConfigArray = [];
        }else{
            propsRightSwipeConfigArray = props.rightSwipeConfigArray;
        }
        this.state = {
            // 商品对象
            goodsObj: props.goodsObj,
            url: props.url,
            rightSwipeConfigArray : propsRightSwipeConfigArray
        };
    }

	render () {
        let {goodsObj, rightSwipeConfigArray, url} = this.state;
		return (
            <SwipeAction style={{ backgroundColor: 'gray' }}
                         autoClose
                         right={rightSwipeConfigArray}
            >
                 <Link to={ url }>
                     <div style={{"background": "#fff"}} >
                         <img src='src/assets/yay.jpg' style={this.imageStyle}/>
                         <div style={{"marginLeft": "100px"}}>
                             <div style={{"fontSize":"16px","lineHeight":"1.8"}}>{goodsObj.goodsName}-{goodsObj.goodsId}</div>
                             <div style={{"lineHeight":"1.8"}}>售价:{goodsObj.salePrice}</div>
                             <div style={{"clear": "both"}}></div>
                         </div>
                     </div>
                 </Link>
            </SwipeAction>
		);
	}
}
export default GoodsListItemComp;

SwipeAction 的属性right 定义的方法,最终还是来自父组件传递过来的,方法中的this是指向的父组件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值