【问题标题】:stay in active button when click on it in react js ( functional component )在反应 js(功能组件)中单击它时保持活动按钮
【发布时间】:2021-06-27 12:25:06
【问题描述】:

我想留在我点击按钮的部分,并且按钮也留在背景和颜色中,以显示你在例如早餐或午餐或晚餐或...部分

const Categories = ({ categories, filter }) => {
  return (
    <div className="btn-container">
      {categories.map((categorie, index) => {
        return (
          <button
            type="button"
            className="filter-btn"
            key={index}
            onClick={() => filter(categorie)}
          >
            {categorie}
          </button>
        );
      })}
    </div>
  );
};

all sectionbreakfast sectionshakes section

【问题讨论】:

  • 我不明白这个问题。
  • 当我单击所有按钮或早餐按钮上的示例时,按钮处于活动状态并在此部分中向用户显示您的
  • 看看之前提出的类似问题的第一个答案:stackoverflow.com/questions/61835816/…

标签: javascript reactjs jsx react-functional-component


【解决方案1】:

为此,您必须将值保存在另一个状态,然后运行此映射方法


{categories.filter(name => name.includes(**anotherStatevalue**)).map((categorie, index) => {
        return (
          <button
            type="button"
            className="filter-btn"
            key={index}
            onClick={() => filter(categorie)}
          >
            {categorie}
          </button>
        );
      })}

【讨论】:

  • 谢谢,在这个组件中我应该使用 useState 吗?以及如何节省价值?
猜你喜欢
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 2015-07-27
相关资源
最近更新 更多