在ReactJS中添加一个类并从另一个类中删除该类

时间:2019-06-13 07:21:57

标签: reactjs

我想使用ul和li创建一个选项卡,该选项卡将显示与所选内容相同的内容,并应显示一个按钮,该按钮会将我发送到下一个选项卡,并且将用于指示哪个选项卡正在显示给我

render() {
    return (
      <div>

        <ul>
          <li><a href="#tab-menu-1" className="active">Tab 1</a></li>
          <li><a href="#tab-menu-2">Tab 2</a></li>
          <li><a href="#tab-menu-3">Tab 3</a></li>
        </ul>
        <div id="tab-menu-1">Tab 1 Content <button>Move to Tab 2</button></div>

        //in default it should show the tab 1 content only and When i Press this button on above Div it should be moved to next tab and the class name will be transfer to the tab 2 on <li>

        <div id="tab-menu-2">Tab 2 Content <button>Move to Tab 2</button></div>

        //in default it should show the tab 2 content only and When i Press this button on above Div it should be moved to next tab and the class name will be transfer to the tab 3 on <li>

        <div id="tab-menu-3">Tab 3 Content <button>Move to Tab 2</button></div>

        //in default it should show the tab 3 content only and When i Press this button on above Div it should be moved to next tab and the class name will be transfer to the tab 1 on <li>

      </div>
    );
  }

1 个答案:

答案 0 :(得分:0)

您可以在组件的状态上保存activeIndex id,并有条件地呈现className。

class Demo extends React.Component {

  state = {
    activeIdx: 0
  };

  render() {
    return (
      <div>

        <ul>
          <li><a href="#tab-menu-1" className={this.state.activeIdx === 0 ? 'active' : ''} data-index={0} onClick={this.handleChange}>Tab 1</a>
          </li>
          <li><a href="#tab-menu-2" className={this.state.activeIdx === 1 ? 'active' : ''} data-index={0} onClick={this.handleChange}>Tab 2</a></li>
          <li><a href="#tab-menu-3" className={this.state.activeIdx === 2 ? 'active' : ''} data-index={0} onClick={this.handleChange}>Tab 3</a></li>
        </ul>
        <div id="tab-menu-1">Tab 1 Content <button>Move to Tab 2</button></div>

        <div id="tab-menu-2">Tab 2 Content <button>Move to Tab 2</button></div>

        <div id="tab-menu-3">Tab 3 Content <button>Move to Tab 2</button></div>

      </div>
    );
  }

  handleChange = (evnt) => {
    this.setState({activeIdx: evnt.target.data.index});
  } 
}

一种更好的方法是从数组中生成项目列表。

class Demo extends React.Component {
  state = {
    activeIdx: 0,
    list: ["content 1", "content 2", "content 3"]
  };

  render() {
    return (
      <div>
        <ul>
          {this.state.list.map((item, index) => (
            <li key={item}>
              <a
                href={`#tab-menu-${index + 1}`}
                className={this.state.activeIdx === index && "active"}
                data-index={index}
                onClick={this.handleChange}
              >
                Tab {index + 1}
              </a>
            </li>
          ))}
        </ul>
        {this.state.list.map((item, index) => (
          <div id={`tab-menu-${index + 1}`} key={item}>
            {item}
            <button>Move to Tab {index + 2}</button>
          </div>
        ))}
      </div>
    );
  }

  handleChange = evnt => {
    this.setState({ activeIdx: evnt.target.data.index });
  };
}