简体   繁体   English

如何根据在 React 中单击哪个按钮来切换 div class?

[英]How do I toggle div class depending on which button has been clicked in React?

I'm creating a product page in my React (ES6) app to display different product features.我正在我的 React (ES6) 应用程序中创建一个产品页面来显示不同的产品功能。 There will be a button for each feature and a div for each feature displaying the information.每个功能都有一个按钮,每个功能都有一个显示信息的 div。

When a button is clicked it should toggle an active class to the relevant content div, however I'm fairly new to React so I'm unsure how this would be done with regards to toggling the class.单击按钮时,它应该将活动的 class 切换到相关的内容 div,但是我对 React 还很陌生,所以我不确定如何切换 class。

My code as it stands is as follows:我的代码如下:

import React, { useContext } from "react";
import { Link } from "react-router-dom";
import '../App.scss';
import ProductData from "./data/ProductData";
import { ChoicesContext } from "../context/ChoicesProvider";

class Product extends React.Component {

  constructor(props) {
      super(props);
      this.toggleClass= this.toggleClass.bind(this);
      this.state = {
          active: false,
      };
  }
  toggleClass() {
      const currentState = this.state.active;
      this.setState({ active: !currentState });
  };

  static contextType = ChoicesContext;

  render() {
    const { choices } = this.context;
    const CurrentProduct = ProductData.filter(x => x.name === choices.productSelected);

    return (
      <>

        <div className="product wrapper d-md-flex">

          <main>

            <Link
              {/* Need this to toggle active class to <Overlay text={'Overlay 1'} /> */}
              className="btn-reverse overlay"
            >TOC</Link>

            <Link
              {/* Need this to toggle active class to <Overlay text={'Overlay 2'} /> */}
              className="btn-reverse overlay"
            >Puresure</Link>

            <Overlay text={'Overlay 1'} />
            <Overlay text={'Overlay 2'} />

          </main>
        </div>
      </>
    )
  }
}
export default Product;

class Overlay extends React.Component {
  render() {
    return (
      <div className="overlay">
        <h2>{this.props.text}</h2>
      </div>
    )
  }
}

i recommend you to use npm package classNames我建议你使用 npm package classNames

import classNames from 'classnames';

...
const classes = classNames('btn-reverse', 'overlay', {
  'active': this.state.active
})

...
<div className={classes}> .... </div>

It works by the next scenario: if this.state.active === true, then classes will be 'btn-reverse overlay active' else 'btn-reverse overlay'它适用于下一个场景:如果 this.state.active === true,则类将是“btn-reverse overlay active”否则为“btn-reverse overlay”

Also, I can recommend you don't filter your products in render, try to avoid it.另外,我可以建议您不要在渲染中过滤您的产品,尽量避免它。 Because no matter why your component will re-render, you always will be filter products因为无论你的组件为什么会重新渲染,你总是过滤产品

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM