简体   繁体   English

做出反应。 如何在点击时在元素中添加 class?

[英]React. How to add class in element on click?

I'm new to React.我是反应新手。 Currently I'm creating one app in learning purposes.目前我正在创建一个用于学习目的的应用程序。 I'm stuck trying to change color of button on click.我被困在试图改变点击按钮的颜色。 When I click on a button, the color of all the buttons are changed.当我点击一个按钮时,所有按钮的颜色都会改变。 But I need to have just one button active, while others are not active.但我只需要激活一个按钮,而其他按钮不激活。 When I click on different button, the previous active button loses its class and become inactive.当我点击不同的按钮时,前一个活动按钮失去其 class 并变为非活动状态。 Could you help me out?你能帮帮我吗?

state = {
  clicked: false
};

timeChangeHandler = () => {
  this.setState({ clicked: true });
};

render() {
  return (
    <div>
      <button
        type="button"
        className={
          this.state.clicked
            ? "list-group-item list-group-item-action active"
            : "list-group-item list-group-item-action"
        }
        onClick={this.timeChangeHandler}
      >
        8:00
      </button>

      <button
        type="button"
        className={
          this.state.clicked
            ? "list-group-item list-group-item-action active"
            : "list-group-item list-group-item-action"
        }
        onClick={this.timeChangeHandler}
      >
        9:00
      </button>
    </div>
  );
}

Your solution would look something like this.您的解决方案看起来像这样。

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            activeButton: 0
        };
    }

    render() {
        const { activeButton } = this.state;

        return (
            <div>
                <button
                    className={ activeButton === 0 && "active" }
                    onClick={() => this.setState({activeButton: 0})}
                />

                <button
                    className={ activeButton === 1 && "active" }
                    onClick={() => this.setState({activeButton: 1})}
                />

                <button
                    className={ activeButton === 2 && "active" }
                    onClick={() => this.setState({activeButton: 2})}
                />
            </div>
        );
    }
}

Here is a simple example of how you can do it.这是一个简单的示例,说明如何做到这一点。 I map the buttons and use index as the clicked value.我 map 按钮并使用索引作为点击值。 But, if you are using your buttons statically as in your code you can use @Kyle's solution.但是,如果您在代码中静态使用按钮,则可以使用@Kyle 的解决方案。

 const buttons = [ { name: "foo" }, { name: "bar" }, { name: "baz" } ]; class App extends React.Component { state = { clicked: null }; handleClick = id => this.setState({ clicked: id }); render() { return ( <div> {buttons.map((button, index) => ( <button className={this.state.clicked === index && "clicked"} onClick={() => this.handleClick(index)} > {button.name} </button> ))} </div> ); } } ReactDOM.render(<App />, document.getElementById("root"));
 .clicked { background-color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root" />

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

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