gpt4 book ai didi

javascript - react : Getting active element name from
    in to the header

转载 作者:行者123 更新时间:2023-11-29 14:41:03 24 4
gpt4 key购买 nike

我正在尝试为移动设备构建一个长导航栏,并且我正在以 Accordion 风格开发它。第一个 View 将具有当前事件的链接名称。用户单击此事件链接名称时,它会在下方展开并列出其他可用链接。

React.createClass({
getInitialState: function() {
return {
tab: false,
navState: "nav-closed"
}
},
handleClick: function() {
if (this.state.tab) {
this.setState({
tab: false,
navState: "nav-closed"
})
} else {
this.setState({
tab: true,
navState: "nav-open"
})
}
},
render: function() {
return (<div>
<div className ="navbar-container">
<div onClick = {this.handleClick}>HEADER`
/* (should display active element name here) */
</div>
<div className = {this.state.navState}>
<ul role="nav">
<li onClick = {this.handleClick}><Link activeClassName="active" to="path1">PATH 1</Link></li>
<li onClick = {this.handleClick}><Link activeClassName="active" to="/path2">PATH 2</Link></li>
</ul>
</div>
</div>
{this.props.children}
</div>)
}
})

在代码示例中,我试图获取当前事件的链接(PATH1 或 PATH2)来代替“HEADER”

有什么我应该关注的建议或意见吗?

最佳答案

您可以通过访问作为参数传递给 handleClick 方法的事件对象的 currentTarget.interHtml 属性来获取点击链接的文本:

handleClick: function(e) {
console.log(e.currentTarget.innerHTML); // should log PATH 1 or PATH 2
//...
}

所以我可以这样建议:​​

React.createClass({
getInitialState: function() {
return {
heder: '',
tab: false,
navState: "nav-closed"
}
},
handleClick: function(e) {
let header = e.currentTarget.innerHTML;
if (this.state.tab) {
this.setState({
tab: false,
navState: "nav-closed",
header
})
} else {
this.setState({
tab: true,
navState: "nav-open",
header
})
}
},
render: function() {
return (<div>
<div className ="navbar-container">
<div onClick = {this.handleClick}>{this.state.header}
</div>
<div className = {this.state.navState}>
<ul role="nav">
<li onClick = {this.handleClick}><Link activeClassName="active" to="path1">PATH 1</Link></li>
<li onClick = {this.handleClick}><Link activeClassName="active" to="/path2">PATH 2</Link></li>
</ul>
</div>
</div>
{this.props.children}
</div>)
}
})

关于javascript - react : Getting active element name from <ul> in to the header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38209986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com