反应孩子调用父处理程序函数,父必须将`this`传递给孩子吗?

时间:2018-07-18 16:28:40

标签: javascript reactjs inheritance

我所拥有的是项目列表,其中每个select p.PriceZoneID, p.Name, max(p.Units) as Units, p.Date, p.CategoryName from ( Select ps.PriceZoneID, ps.Name, sum (ash.Sales) as Units, ash.date, es.CategoryName FROM AggregatedSalesHistory as ash JOIN v_EnterpriseStructure as es ON es.ProductSID = ash.ProductSID JOIN PriceZone as ps ON ps.PriceZoneID = ash.PriceZoneID WHERE DepartmentName ='Dairy' and ash.Date >= '12-17-2014' and ash.date<= '12-31-2014' GROUP BY ps.PriceZoneID, ps.Name, es.CategoryName, ash.Sales, ash.Date ) p group by p.PriceZoneID, p.Name, p.Date, p.CategoryName --If category isn't there remove the field and the group by category 组件都有一个删除按钮,该按钮将调用Item组件中的deleteItem函数以更新其州。 List函数将需要绑定到其的父实例来调用deleteItem

目前,当this.setState渲染parent={this}时,我将deleteHandler={this.deleteItem}List一起传递。这样,当Item调用处理程序时,它将可以访问父实例。

Item

但这感觉没有必要,我想知道是否有更好的方法来实现我想做的事情。 (例如,孩子和父母之间的某种内置链接,或者更好的方法来传递/调用该函数)

codepen

子组件

onClick={this.props.deleteHandler.bind(
    this.props.parent,
    this.props._key
)}

父组件

class Item extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return (
            <li>
                {this.props.value}
                <button
                    className="delete"
                    onClick={this.props.deleteHandler.bind(
                        this.props.parent,
                        this.props._key
                    )}
                >
                    x
                </button>
            </li>
        );
    }
}

谢谢!

0 个答案:

没有答案
相关问题