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 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:
|
<<: Introduction to generating Kubernetes certificates using OpenSSL
>>: Nginx sample code for implementing dynamic and static separation
offset Offset is the offset. Using the offset ser...
<iframe src=”test.jsp” width=”100″ height=”50″...
Table of contents 1 Indicators in stress testing ...
Introduction to jsvc In production, Tomcat should...
It is very easy to delete a table in MySQL, but y...
<br />Related articles: innerHTML HTML DOM i...
I believe everyone has used JD. There is a very c...
The docker exec command can execute commands in a...
gzip is a command often used in Linux systems to ...
Add table fields alter table table1 add transacto...
1. Check whether the existing nginx supports ipv6...
Introduction to Nginx Nginx ("engine x"...
Summary: Configure nginx reverse proxy jira and i...
1. 85% of ads go unread <br />Interpretatio...
This article example shares the specific code of ...