[英]React utilizing child event listener in parent
I am struggling with wrapping my head around sending information from an onClick event listener in my child component to my parent component. 我正在竭尽全力地将信息从子组件中的onClick事件侦听器发送到父组件。 I am rendering an array of children, and I want each to have an event listener, so that
onClick
, the name of that child will be set as the "selected" parameter in my parent state. 我正在渲染一个孩子数组,我希望每个孩子都有一个事件侦听器,以便在我的父状态下将该孩子的名称
onClick
设置为“ selected”参数。
This is my child component. 这是我的孩子部分。
var DropDownLink = React.createClass({
render: function(){
var dropDownVisible;
if (this.props.visible){
dropDownVisible="dropdownoption"
} else {
dropDownVisible="dropdownoption hide"
}
return (
<div onClick={this.props.onClick} className={dropDownVisible}>{this.props.name}</div>
);
}
});
And this is my parent component: 这是我的父组件:
var Dropdown = React.createClass({
getInitialState: function() {
return {
listVisible: false,
selected: this.props.items[0].name,
};
},
show: function() {
this.setState({ listVisible: true });
document.addEventListener("click", this.hide);
},
hide: function() {
this.setState({ listVisible: false });
document.removeEventListener("click", this.hide);
},
generateDropDown: function(item) {
return <DropDownLink name={item.name} visible={this.state.listVisible}/>
},
render: function() {
var items = this.props.items.map(this.generateDropDown);
var dropdowncontain;
if (this.state.listVisible) {
dropdowncontain="dropdowncontainer";
} else {
dropdowncontain="dropdowncontainer hide";
}
return (
<span className="roledropdown" onClick={this.show}>
<span className="roleselected">{this.state.selected}</span>
<i className="fa fa-caret-down clickdown"></i>
<div className={dropdowncontain}>{items}</div>
</span>
)
}
});
I believe I have set up the child component correctly based on some examples I saw, however what is confusing me is how I should write the function in the parent that handles each child index, given that I am already mapping the array using my generateDropDown function. 我相信我已经根据一些示例正确设置了子组件,但是令我感到困惑的是,鉴于我已经使用generateDropDown函数映射了数组,我该如何在处理每个子索引的父组件中编写函数。 Ideally, I want to just modify the generateDropDown function, to include this functionality, but I am new to React and struggling to understand how to.
理想情况下,我只想修改generateDropDown函数以包括此功能,但是我对React还是陌生的,并且难以理解如何使用。
Here's an example of what you could change in your Dropdown
component. 这是您可以在
Dropdown
组件中进行更改的示例。 Essentially, pass in a onClick handler to your children with some unique identifier bound to each. 本质上,将onClick处理程序传递给您的孩子,每个孩子绑定一些唯一的标识符。 In this example, it is bound to the item.name because that is in your original code.
在此示例中,它绑定到item.name,因为它在原始代码中。 You could use something else from the
item
object, though. 不过,您可以使用
item
对象中的其他内容。
handleChildClick: function(childName, event){
// One of the DropDownLink components was clicked.
// Handle that click here.
},
generateDropDown: function(item){
return <DropDownLink name={item.name} onClick={this.handleChildClick.bind(item.name)} visible={this.state.listVisible}/>
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.