javascript - 在 Reactjs 中创建一个可折叠的 ul li
问题描述
react
创建可折叠ul/li
列表的最佳做法是什么?我希望列表在悬停时展开并在未悬停时关闭。
解决方案
悬停在其上的可折叠手风琴。
jsFiddle 上的工作示例 - https://jsfiddle.net/op088krw/98/
HTML
<div id="app"></div>
JS
const PRODUCTS = [
{"id": 1, "name": "Bag of suck", "price": 100, "details": "You don't want to own this!"},
{"id": 2, "name": "Bag of luck", "price": 200, "details": "You might want to own this!"},
{"id": 3, "name": "Bag of fuck", "price": 300, "details": "You really want to own this!"}
];
var ItemList = React.createClass({
getInitialState(){
return {active: null}
},
handleClick(i){
return (e) => {
let active = this.state.active === i ? null : i
this.setState({active: active})
}
},
display(i){
return this.state.active === i ? 'block' : 'none'
},
liClass(i){
return this.state.active === i ? 'active' : 'inactive'
},
Item(props, i) {
return(
<li key={i} onMouseEnter={this.handleClick(i)}>
<span>
{props.name + '(' + props.price + ')'}
</span>
<div style={{display: this.display(i)}}>
{props.details}
</div>
</li>
)
},
render(){
let {products} = this.props
return (
<ul>
{products.map(this.Item)}
</ul>
)
}
})
const App = React.createClass({
getInitialState(){
return {active: null}
},
render(){
return (
<div>
<h1> Hover over li items </h1>
<ItemList products={PRODUCTS} />
</div>
)
}
})
ReactDOM.render(
<App />,
document.getElementById("app")
)
推荐阅读
- javascript - 如何递归爬取页面,抓取链接,关注链接,然后再次抓取并在节点js中导出?
- c# - 在 LINQPad 中托管基于 SwaggerUI 的服务
- node.js - 用 ANSI 转义码覆盖 REPL 中复制粘贴的前几行
- python - 为什么我在更改 NaN 值后得到 RecursionError
- python - 从另一个列表中每隔 N 个索引添加一个列表,并根据元素的数量重复
- python - TIF 到 JPEG 转换 - 没有这样的文件或目录
- r - 如何删除2列中具有重复值的行但在R中保留具有特定因子的指定行
- rust - Rust 语法 `if let [.., last] = self ...` 是什么意思?
- java - jayway jsonpath 在读取不存在的 json 路径时抛出错误,尽管配置为
- css - 链关键帧动画