React antd tabs切換造成子組件重復(fù)刷新
描述:
Tabs組件在來回切換的過程中,造成TabPane中包含的相同子組件重復(fù)渲染,例如:
<Tabs
activeKey={tabActiveKey}
onChange={(key: string) => this.changeTab(key)}
type="card"
>
<TabPane tab={"對(duì)外授權(quán)"} key="/authed-by-me">
<AuthedCollections
collectionType={this.collectionType}
/>
</TabPane>
<TabPane tab={"申請(qǐng)權(quán)限"} 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
})
};
分析:
在Tabs的onChange監(jiān)聽函數(shù)changeTab中調(diào)用setState,造成頁面重新render。antd 的策略是,只渲染當(dāng)前的。當(dāng)用戶切換過后,不刪除之前渲染過的節(jié)點(diǎn)。所以點(diǎn)擊切換會(huì)逐漸增多。為的是防止用戶在 mount 階段調(diào)用異步請(qǐng)求導(dǎo)致切換時(shí)反復(fù)渲染。所以 render 數(shù)量隨著上層刷新而整體刷新并增加是預(yù)期行為。
解決方案:
運(yùn)用react的條件渲染,在每一次tab切換的時(shí)候?qū)⑸蟼€(gè)頁面移出Dom樹
<Tabs
activeKey={tabActiveKey}
onChange={(key: string) => this.changeTab(key)}
type="card"
>
<TabPane tab={"對(duì)外授權(quán)"} key="">
{
this.collectionType === CollectionEnums.AUTHED_TO_GRANT &&
<AuthedCollections
collectionType={this.collectionType}
/>
}
</TabPane>
<TabPane tab={"申請(qǐng)權(quán)限"} key="/authed-to-me">
{
this.collectionType === CollectionEnums.AUTHED_TO_APPLY &&
<AuthedCollections
collectionType={this.collectionType}
/>
}
</TabPane>
</Tabs>
Antd Tabs 當(dāng)前頁面來回切換 表單數(shù)據(jù)不清空(或者清空)
清空表單的辦法是this.props.form.resetFields();
但是本篇文章主要講解不清空
靈活使用 display:none 就可以避免切換的時(shí)候表單數(shù)據(jù)被setState重新渲染清空掉 (即切換tab項(xiàng),不清空表單)


查詢區(qū)域
{/* 查詢區(qū)域 */}
<div className="search-form-area">
{
<div style={{ display: activeKey === '1' ? 'block' : 'none' }}><SearchFieldForm // 項(xiàng)目角度
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 // 產(chǎn)品角度
ref={(form) => this.PrdSearchForm = form}
submitFuc={this.getProjPage}
fieldsData={prdSearchData}
colNum={4}
diyItemLayout={{ labelCol: { span: 4 }, wrapperCol: { span: 17 } }}
/></div>
}
</div>
列表區(qū)域
{/* 列表區(qū)域 */}
<div style={{ height: tableHeight + 100 }} className='myProjTable'>
<Tabs defaultActiveKey="1" onChange={this.handleTabsChange}>
<TabPane tab="項(xiàng)目角度" 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="產(chǎn)品角度" 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>
到此這篇關(guān)于React antd tabs切換造成子組件重復(fù)刷新的文章就介紹到這了,更多相關(guān)antd tabs重復(fù)刷新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React styled-components設(shè)置組件屬性的方法
這篇文章主要介紹了styled-components設(shè)置組件屬性的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
用React Native制作一個(gè)簡(jiǎn)單的游戲引擎
今天給大家分享的是使用React Native制作一個(gè)簡(jiǎn)單的游戲,這個(gè)游戲可以跨平臺(tái)操作,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)React Native游戲相關(guān)知識(shí)感興趣的朋友一起看看吧2021-05-05
React中memo useCallback useMemo方法作用及使用場(chǎng)景
這篇文章主要為大家介紹了React中三個(gè)hooks方法memo useCallback useMemo的作用及使用場(chǎng)景示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-03-03
用react-redux實(shí)現(xiàn)react組件之間數(shù)據(jù)共享的方法
這篇文章主要介紹了用react-redux實(shí)現(xiàn)react組件之間數(shù)據(jù)共享的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
react中form.setFieldvalue數(shù)據(jù)回填時(shí) value和text不對(duì)應(yīng)的問題及解決方法
這篇文章主要介紹了react中form.setFieldvalue數(shù)據(jù)回填時(shí) value和text不對(duì)應(yīng)的問題及解決方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
react性能優(yōu)化useMemo與useCallback使用對(duì)比詳解
這篇文章主要為大家介紹了react性能優(yōu)化useMemo與useCallback使用對(duì)比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

