【发布时间】:2021-04-14 18:34:21
【问题描述】:
使用地图功能并显示的按钮不止一个。 想要更改我单击的按钮的背景颜色。而其他人则希望它保持原样。 当我再次单击另一个按钮时,仅更改该按钮的 BG 颜色。 有两个文件。一个组件 id 使用 map 函数定义按钮组件,另一个是按钮组件。 主要组件
state = {
categories: [],
selectedCategory: null,
value: 'test',
clicked1: false,
}
categorySelectedHandler = (id ,e) => {
this.setState({ selectedCategory: id });
}
const categoriesName = this.state.categories.map((category ,index) => {
// console.log("The current iteration is: " + index);
let visible_in_pricing_page = category.visible_in_pricing_page
if (visible_in_pricing_page) {
return <CategoryBtn
index = {index}
name={category.title}
key={category.id}
selectedId = {this.state.selectedCategory}
clicked={() => this.categorySelectedHandler(category.id)}
/>
}
});
CategoryBtn 组件 -
import React, { Component } from 'react';
import classes from './price-category-btn.css';
const CategoryBtn = props => {
return (
<div style={{display:"inline"}} >
<a>
<button
className= { classes.category_btn }
onClick={props.clicked}>{props.name}</button></a>
</div>
)
}
export default CategoryBtn;
【问题讨论】:
标签: javascript reactjs button background-color