react.js按钮的活动类

发布于 2025-02-10 03:15:19 字数 2493 浏览 0 评论 0原文

我对活动按钮有问题。在Stackoverflow上找不到解决方案。

我有一家商店,并且有用于选择产品特征的按钮。目前,我设法将一个值保存在该州,但是如果属性类别相同,则启用了两个产品的活动按钮。

  • 感谢您的帮助!

app.js

class App extends Component {
  Data;
  constructor(props) {
    super(props);
    this.state = {
      activeName: null,
      activeColor: null
    };
    this.handleAttribute = this.handleAttribute.bind(this);
    this.colorAttribute = this.colorAttribute.bind(this);
}
handleAttribute(e) {
    e.preventDefault();
    this.setState({activeName: e.currentTarget.id });
  }
  colorAttribute(e) {
    e.preventDefault();
    this.setState({activeColor: e.currentTarget.id });
  }

cart.js

     <div className={s.attributesContainer}>{products.attributes.map((attributes) => {
                      
                      return (
                        <div>
                          <div key={attributes.id} className={s.attributesName} >{attributes.id}:</div>
                          {attributes.items.map((itemss) => {
                            return ( 
                              <div 
                               className={s.attributesButton}>
                                <div className={attributes.type === "text" ? (itemss.id === this.props.activeName?s.itemsAttrributesActive:s.itemsAttrributes): (itemss.id ===this.props.activeColor?s.itemsAttrributesColorActive:s.itemsAttrributesColor)} style={{ background: itemss.value }}
                                onClick={attributes.id?attributes.type=== "text"? e => this.props.handleAttribute(e) : e =>this.props.colorAttribute(e):null} id={itemss.id}>
                              {console.log(this.props.activeName)}
                                  <button className={attributes.type === "text" ?(itemss.id === this.props.activeName?s.buttonInfoActive:s.buttonInfo):s.buttonInfo} >
                                    {attributes.id != "Color" ? itemss.displayValue : null}
                                  </button>
                                </div>
                                
                              </div>
                              )
                          })}
                        </div>
                      )
                    })}</div>
                  </div>
                </div>
                : null))))}

I have a problem with the active button. Didn't find a solution on stackoverflow.

I have a store and there are buttons for selecting product characteristics. At the moment, I managed to make one value be saved in the state, but if the attribute category is the same, then the active button is enabled for both products.

  • Thank you for your help!

App.js

class App extends Component {
  Data;
  constructor(props) {
    super(props);
    this.state = {
      activeName: null,
      activeColor: null
    };
    this.handleAttribute = this.handleAttribute.bind(this);
    this.colorAttribute = this.colorAttribute.bind(this);
}
handleAttribute(e) {
    e.preventDefault();
    this.setState({activeName: e.currentTarget.id });
  }
  colorAttribute(e) {
    e.preventDefault();
    this.setState({activeColor: e.currentTarget.id });
  }

Cart.js

     <div className={s.attributesContainer}>{products.attributes.map((attributes) => {
                      
                      return (
                        <div>
                          <div key={attributes.id} className={s.attributesName} >{attributes.id}:</div>
                          {attributes.items.map((itemss) => {
                            return ( 
                              <div 
                               className={s.attributesButton}>
                                <div className={attributes.type === "text" ? (itemss.id === this.props.activeName?s.itemsAttrributesActive:s.itemsAttrributes): (itemss.id ===this.props.activeColor?s.itemsAttrributesColorActive:s.itemsAttrributesColor)} style={{ background: itemss.value }}
                                onClick={attributes.id?attributes.type=== "text"? e => this.props.handleAttribute(e) : e =>this.props.colorAttribute(e):null} id={itemss.id}>
                              {console.log(this.props.activeName)}
                                  <button className={attributes.type === "text" ?(itemss.id === this.props.activeName?s.buttonInfoActive:s.buttonInfo):s.buttonInfo} >
                                    {attributes.id != "Color" ? itemss.displayValue : null}
                                  </button>
                                </div>
                                
                              </div>
                              )
                          })}
                        </div>
                      )
                    })}</div>
                  </div>
                </div>
                : null))))}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虚拟世界 2025-02-17 03:15:19

我认为那是因为您的身份证不是唯一的。如果您使用唯一的ID,则应按预期工作。如果我认为您正在通过产品循环,那么使用索引可以提供帮助。您可以在下面参考我使用产品索引和属性索引来创建唯一ID的 $ {PRODUCTSINDEX} - $ {attributeIndex} - $ {pockts.id}

<div>
  {
    data.category.products.map((products, productsIndex) => {
      return <div className={s.attributesContainer}>
        {products.attributes.map((attributes, attributeIndex) => {
          return (
            <div>
              <div key={attributes.id} className={s.attributesName} >{attributes.id}:</div>
              {attributes.items.map((itemss) => {
                return (
                  <div className={s.attributesButton}>
                    <div className={attributes.type === "text" ? (`${productsIndex}-${attributeIndex}-${itemss.id}` === this.props.activeName ? s.itemsAttrributesActive : s.itemsAttrributes) : (`${productsIndex}-${attributeIndex}-${itemss.id}` === this.props.activeColor ? s.itemsAttrributesColorActive : s.itemsAttrributesColor)} style={{ background: itemss.value }}
                      onClick={attributes.id ? attributes.type === "text" ? e => this.props.handleAttribute(e) : e => this.props.colorAttribute(e) : null} id={`${productsIndex}-${attributeIndex}-${itemss.id}`}>
                      {console.log(this.props.activeName)}
                      <button className={attributes.type === "text" ? (`${productsIndex}-${attributeIndex}-${itemss.id}` === this.props.activeName ? s.buttonInfoActive : s.buttonInfo) : s.buttonInfo} >
                        {attributes.id != "Color" ? itemss.displayValue : null}
                      </button>
                    </div>
                  </div>
                )
              })}
            </div>
          )
        })}
      </div>
    })
  }
</div>

I think that's because your id's are not unique. If you use unique id's then it should work as expected. If I assume that you are looping through products then using index can help. You can refer below code where I have used product index and attributes index to create unique id's ${productsIndex}-${attributeIndex}-${itemss.id}

<div>
  {
    data.category.products.map((products, productsIndex) => {
      return <div className={s.attributesContainer}>
        {products.attributes.map((attributes, attributeIndex) => {
          return (
            <div>
              <div key={attributes.id} className={s.attributesName} >{attributes.id}:</div>
              {attributes.items.map((itemss) => {
                return (
                  <div className={s.attributesButton}>
                    <div className={attributes.type === "text" ? (`${productsIndex}-${attributeIndex}-${itemss.id}` === this.props.activeName ? s.itemsAttrributesActive : s.itemsAttrributes) : (`${productsIndex}-${attributeIndex}-${itemss.id}` === this.props.activeColor ? s.itemsAttrributesColorActive : s.itemsAttrributesColor)} style={{ background: itemss.value }}
                      onClick={attributes.id ? attributes.type === "text" ? e => this.props.handleAttribute(e) : e => this.props.colorAttribute(e) : null} id={`${productsIndex}-${attributeIndex}-${itemss.id}`}>
                      {console.log(this.props.activeName)}
                      <button className={attributes.type === "text" ? (`${productsIndex}-${attributeIndex}-${itemss.id}` === this.props.activeName ? s.buttonInfoActive : s.buttonInfo) : s.buttonInfo} >
                        {attributes.id != "Color" ? itemss.displayValue : null}
                      </button>
                    </div>
                  </div>
                )
              })}
            </div>
          )
        })}
      </div>
    })
  }
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文