最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - All React-bootstrap Tabs render function gets called every time regardless of which tab was selected - Stack Overfl

programmeradmin2浏览0评论

The following is my code:

var WholeTab=React.createClass                                                 
({                                                                             
  getInitialState:function()                                                   
  {                                                                            
    return {                                                                   
      key: 1                                                                   
    };                                                                         
  },                                                                           
  handleSelect:function(key)                                                   
  {                                                                            
    this.setState({key});                                                      
  },                                                                           
  render:function()                                                            
  {                                                                            
    return (                                                                   
      <Tabs defaultActiveKey={1} id="controlled-tab-example">                  
        <Tab eventKey={1} title="Tab 1"><One/></Tab>                           
        <Tab eventKey={2} title="Tab 2"><Two/></Tab>                           
        <Tab eventKey={3} title="Tab 3"><Three/></Tab>                         
      </Tabs>                                                                  
    );                                                                         
  }                                                                            
});                                                                            
var One=React.createClass({                                                    
  render:function(){                                                           
    alert("one");                                                              
    return(                                                                    
      <p>We are here</p>                                                       
    );                                                                         
  }                                                                            
});                                                                            
var Two=React.createClass({                                                    
  render:function(){                                                           
    alert("two");                                                              
    return(                                                                    
      <p>We are here</p>                                                       
    );                                                                         
  }                                                                            
});                                                                            
var Three=React.createClass({                                                  
  render:function(){                                                           
    alert("three");                                                            
    return(                                                                    
      <p>We are here</p>                                                       
    );                                                                         
  }                                                                            
});     

The thing which I have noticed is, whenever there is a change of a tab, the render function of all the tabs get called, regardless they were selected or not, and this happens not once, but 3 times. This is just a lame example, but if i have a lot of content in each and every tab, the behaviour I described above certainly affects the performance of the web-page and makes it slow. Am i doing anything wrong or is this a bug with react-bootstrap tabs because logically the render function of all the tabs should not get called on each and every change.

The following is my code:

var WholeTab=React.createClass                                                 
({                                                                             
  getInitialState:function()                                                   
  {                                                                            
    return {                                                                   
      key: 1                                                                   
    };                                                                         
  },                                                                           
  handleSelect:function(key)                                                   
  {                                                                            
    this.setState({key});                                                      
  },                                                                           
  render:function()                                                            
  {                                                                            
    return (                                                                   
      <Tabs defaultActiveKey={1} id="controlled-tab-example">                  
        <Tab eventKey={1} title="Tab 1"><One/></Tab>                           
        <Tab eventKey={2} title="Tab 2"><Two/></Tab>                           
        <Tab eventKey={3} title="Tab 3"><Three/></Tab>                         
      </Tabs>                                                                  
    );                                                                         
  }                                                                            
});                                                                            
var One=React.createClass({                                                    
  render:function(){                                                           
    alert("one");                                                              
    return(                                                                    
      <p>We are here</p>                                                       
    );                                                                         
  }                                                                            
});                                                                            
var Two=React.createClass({                                                    
  render:function(){                                                           
    alert("two");                                                              
    return(                                                                    
      <p>We are here</p>                                                       
    );                                                                         
  }                                                                            
});                                                                            
var Three=React.createClass({                                                  
  render:function(){                                                           
    alert("three");                                                            
    return(                                                                    
      <p>We are here</p>                                                       
    );                                                                         
  }                                                                            
});     

The thing which I have noticed is, whenever there is a change of a tab, the render function of all the tabs get called, regardless they were selected or not, and this happens not once, but 3 times. This is just a lame example, but if i have a lot of content in each and every tab, the behaviour I described above certainly affects the performance of the web-page and makes it slow. Am i doing anything wrong or is this a bug with react-bootstrap tabs because logically the render function of all the tabs should not get called on each and every change.

Share Improve this question edited Aug 22, 2016 at 10:31 michele b 1,8652 gold badges20 silver badges28 bronze badges asked Jul 31, 2016 at 22:43 Indraneel BendeIndraneel Bende 3,4865 gold badges25 silver badges41 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

The react-bootstrap Tabs component has a boolean property unmountOnExit which causes the active tab to be the only one actually present in the DOM.

This feature is documented at https://react-bootstrap.github.io/components/tabs/

Even if unmountOnExit is true, when you switch between Tabs, you will still see the child component rendered twice.This is because the transition value renders the component again. You must set transition to false. So to make sure that the child component is rendered only once, you should edit your code like this:

<Tabs
    activeKey={activeKey}
    onSelect={onTabChange}
    unmountOnExit={true}
    mountOnEnter={true}
    transition={false}>

{children}

</Tabs>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论