React antd tabs switching causes repeated refresh of subcomponents

React antd tabs switching causes repeated refresh of subcomponents

describe:

When the Tabs component switches back and forth, the same subcomponents contained in the TabPane are rendered repeatedly, for example:

<Tabs
 activeKey={tabActiveKey}
 onChange={(key: string) => this.changeTab(key)}
 type="card"
>
  <TabPane tab={"External authorization"} key="/authed-by-me">
    <AuthedCollections
        collectionType={this.collectionType}
     />
  </TabPane>
  <TabPane tab={"Apply for permissions"} key="/authed-to-me">
    <AuthedCollections
      collectionType={this.collectionType}
     />
  </TabPane>
</Tabs>


changeTab = (key: string) => {
 this.collectionType = key === '/authed-by-me' ? CollectionEnums.AUTHED_TO_GRANT : CollectionEnums.AUTHED_TO_APPLY;
 this.setState({
  tabActiveKey: key
 })
};

analyze:

Call setState in the onChange listener function changeTab of Tabs, causing the page to be re-rendered. Antd's strategy is to only render the current one. When the user switches, the previously rendered nodes are not deleted. So the number of clicks to switch will gradually increase. This is to prevent users from calling asynchronous requests during the mount phase, which causes repeated rendering when switching. Therefore, it is expected that the number of renders will be refreshed and increased as the upper layer is refreshed.

Solution:

Use react's conditional rendering to move the previous page out of the Dom tree every time the tab is switched

<Tabs
 activeKey={tabActiveKey}
 onChange={(key: string) => this.changeTab(key)}
 type="card"
>
 <TabPane tab={"External authorization"} key="">
 {
 this.collectionType === CollectionEnums.AUTHED_TO_GRANT &&
 <AuthedCollections
 collectionType={this.collectionType}
 />
 }
 </TabPane>
 <TabPane tab={"Apply for permissions"} key="/authed-to-me">
 {
 this.collectionType === CollectionEnums.AUTHED_TO_APPLY &&
 <AuthedCollections
 collectionType={this.collectionType}
 />
 }
 </TabPane>
</Tabs>

Antd Tabs current page switches back and forth, the form data is not cleared (or cleared)

The way to clear the form is this.props.form.resetFields();

However, this article mainly explains

Flexible use of display:none can avoid the form data being re-rendered and cleared by setState when switching (i.e. switching tab items without clearing the form)

Query area

{/* Query area*/}
    <div className="search-form-area">
     {
      <div style={{ display: activeKey === '1' ? 'block' : 'none' }}><SearchFieldForm // Project angle ref={(form) => this.ProjSearchForm = form}
       submitFuc={this.getProjPage}
       fieldsData={projSearchData}
       colNum={4}
       diyItemLayout={{ labelCol: { span: 4 }, wrapperCol: { span: 17 } }}
      // moreSearchData={moreSearchData}
      /></div>
     }
     {
      <div style={{ display: activeKey === '2' ? 'block' : 'none' }}>< SearchFieldForm // Product angle ref={(form) => this.PrdSearchForm = form}
       submitFuc={this.getProjPage}
       fieldsData={prdSearchData}
       colNum={4}
       diyItemLayout={{ labelCol: { span: 4 }, wrapperCol: { span: 17 } }}
      /></div>
     }
    </div>

List Area

 {/* List area */} 
        <div style={{ height: tableHeight + 100 }} className='myProjTable'> 
          <Tabs defaultActiveKey="1" onChange={this.handleTabsChange}> 
            <TabPane tab="Project Angle" key="1" style={{ backgroundColor: '#fff' }}> 
              <CustomTable 
                rowKey='projId'
                size="small"
                style={{ height: tableHeight }}
                columns={columns}
                tableData={projTableData}
                expandedRowRender={this.expandedRowRender}
                pagination={pagination}
                handleTableChange={this.handleTableChange}
                scroll={{ y: tableScrollHeight, x: 1660 }}
                tableRowSelection={this.tableRowSelection}
              />
            </TabPane>
            <TabPane tab="Product Angle" key="2" style={{ backgroundColor: '#fff' }}>
              <CustomTable
                rowKey="projId"
                size="small"
                style={{ height: tableHeight }}
                columns={columnsPrd}
                tableData={prdTableData}
                handleTableChange={this.handleTableChange}
                pagination={pagination}
                scroll={{ y: tableScrollHeight, x: 1800 }}
                tableRowSelection={this.tableRowSelection}
              />
            </TabPane>
          </Tabs>
        </div>

This is the end of this article about React antd tabs switching causing repeated refresh of subcomponents. For more related antd tabs repeated refresh content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • React implements sample code similar to Taobao tab center switching effect
  • React-native method of dynamically switching tab components
  • Sample code for implementing tab switching via event parameter passing in React component
  • Use ReactJS to implement tab page switching, menu bar switching, accordion switching and progress bar effects
  • React handwriting tab switching problem

<<:  Introduction to generating Kubernetes certificates using OpenSSL

>>:  Nginx sample code for implementing dynamic and static separation

Recommend

Introduction to CSS style classification (basic knowledge)

Classification of CSS styles 1. Internal style --...

JS cross-domain solution react configuration reverse proxy

Cross-domain solutions jsonp (simulate get) CORS ...

Detailed explanation of Nginx current limiting configuration

This article uses examples to explain the Nginx c...

Display ellipsis effect when table cell content exceeds (implementation code)

illustrate In front-end development, you often en...

A brief discussion on the maximum number of open files for MySQL system users

What you learn from books is always shallow, and ...

Solutions to MySql crash and service failure to start

I have been in contact with PHP for so long, but ...

Implementation of Vue3 style CSS variable injection

Table of contents summary Basic Example motivatio...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

How to use Spark and Scala to analyze Apache access logs

Install First you need to install Java and Scala,...

How to change apt-get source in Ubuntu 18.04

When using apt-get to install, it will be very sl...

Vue3 (V) Details of integrating HTTP library axios

Table of contents 1. Install axios 2. Use of axio...

How to set the width and height of html table cells

When making web pages, you often encounter the pr...

How to create Apache image using Dockerfile

Table of contents 1. Docker Image 2. Create an in...

Velocity.js implements page scrolling switching effect

Today I will introduce a small Javascript animati...

Detailed tutorial on compiling and installing MySQL 5.7.24 on CentOS7

Table of contents Install Dependencies Install bo...