首页 > 解决方案 > 如何在反应中将图标添加到来自数据库的键

问题描述

我想添加与来自数据库的每个参数相对应的图标......它用于搜索过滤器......这是代码

render() {
    let lugares = Object.keys(this.state.filters).map((key) => {
        // this.state.filters_selected
        return (
            <div className="mb-5 ml-2">
            <Row>
             {Object.values(this.state.icons).map((clave) => {
                        console.log(clave)      
               }
                )}
                <MDBIcon className='mt-1' icon={this.state.icons} />
              <h4 className="ml-1 ">{key}</h4>
                <hr className="accent-4 ml-1 mt-1  mr-5 grey lighten-5" style={{ width: "150px" }} />
                </Row>

图标是您在上面创建的字典 icons:[{'Vehiculo':'car', 'Inmueble':'home','Muebles':'couch', 'Otros':'circle'}]

并且在过滤器中,我有图标的相同键到达..我该怎么做才能使每个图标都绘制在其相应的键中?

标签: reactjs

解决方案


假设icons是包含特定图标的键值对的对象,并且每个keyinfilters都包含每个图标的键,那么您应该可以很简单地icon={this.state.icons}icon={this.state.icons.key}or替换icon={this.state.icons[key]}。希望这可以帮助。


推荐阅读