Same tab content is being displaying twice

▼魔方 西西 提交于 2019-12-25 09:17:20

问题


I have implemented a simple tabs code. I am trying to move the tabs container inside my li tag, since its a new requirement. After changing the code the same tab content is being displaying twice.

  _renderTitles: function () {
    function labels(child, index) {
        var activeClass = (this.state.selected === index ? 'active' : '');

        return (
        <li role="presentation" key={index} className={child.props.liClass}>
            <a href="#" 
            className={`sports-tab-header ${activeClass}`}
            onClick={this.handleClick.bind(this, index)}>
            <h2>{child.props.label}</h2>
            <p className="sports-subtitle">{child.props.subtitle}</p>
          </a>
        </li>
      );
    }
    return (
        <ul className="tabs__labels">
        {this.props.children.map(labels.bind(this))}
      </ul>
    );
  },

回答1:


The content is being shown in both places because of this line:

<div className="tabs__content">
  {this.props.children[this.state.selected]}
</div>

The props.children property holds both children, so you choose to show only the selected content. This is right, except that you're doing this for each child due to the map:

<ul className="tabs__labels">
  {this.props.children.map(labels.bind(this))}
</ul>

The solution that works best is to use the same check you had for this.state.selected === index to your advantage by setting the content only if it's the selected content:

var isActive = this.state.selected === index;
var activeClass = (isActive ? 'active' : '');
// Only set the content if active 
var content = isActive ? this.props.children[this.state.selected] : null;
...

// Show the content, which will be null if not active tab
<div className="tabs__content">
  {content}
</div>

See updated JSFiddle.



来源:https://stackoverflow.com/questions/40498339/same-tab-content-is-being-displaying-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!