3
关注
1787
浏览

ReactJS:如何按顺序映射JSON元素并在点击时显示隐藏的div

查看全部 2 个回答

苹果_love 未验证用户 用户来自于: 广东省
2020-09-16 05:34

我可能会尝试将位置提取到单独的组件中。通过提取它,每个位置负责了解其状态。在你的情况下,这意味着它的可见性(由this.state.isShow控制)。

这里是你如何能做到这一点:

import React from 'react'; 
import { render } from 'react-dom'; 

var places = { 
    library: { 
    location: [ 
     { 
     loc_name: "library1", 
     "desc": "Modern and spacious building" 
     }, 
     { 
     loc_name: "library2", 
     "desc": "A cosy small building" 
     } 
    ] 
    } 
}; 

class Location extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = { isShow: false }; 
    this.handleClick = this.handleClick.bind(this); 
    } 

    handleClick() { 
    this.setState(function (prevState) { 
     return { isShow: !prevState.isShow }; 
    }); 
    } 

    contentClass(isShow) { 
    if (isShow) { 
     return "content"; 
    } 
    return "content invisible"; 
    } 

    render() { 
    return (
     
'control' onClick={this.handleClick}>

{this.props.desc}

this.contentClass(this.state.isShow)}>{this.props.loc_name}
) } } class Toggle extends React.Component { constructor(props) { super(props); } render() { const locations = places.library.location.map(location => { return }) return (
{locations}
); } } render(( ), document.getElementById('root'));

关于作者

问题动态

发布时间
2020-09-15 15:51
更新时间
2022-09-15 15:51
关注人数
3 人关注

相关问题

如何开发一个简单的聊天APP
谐振式传感器是如何产生异常谐振(共振),该怎么解决?
大数据是如何抓到一个人是兴趣的?
如何将点击我的html文件的图像url传递给view.py中的python函数Django
盛元广通在RFID技术在实验室领域用的多吗?行业的前景如何?
VM系列振弦采集模块如何修改通讯速率?
如何让技术再次实现创新升级?
对干只有一个输出端的时钟信号线,如何实现差分布线?
的Javascript:如何加载更新的音频文件并进行播放
如何开发一款聊天app?
个人工作笔记 Powered BY WeCenter V4.1.0 © 2024 粤ICP备2020123311号