请看看我的代码。我尝试限制给定无状态组件的重新渲染,但这样做发现 shouldComponentUpdate 从未被调用。我已经从 styledComponents 中删除了包装器(之前有人报道过这种情况),但仍然没有被调用。除此之外,可以写一篇关于这个函数的捕获的文章
import React from 'react';
import GoogleSearchForm from './RenderGoogleSearchForm.js';
import ButtonWithMessage from './ButtonWithMessage.js';
import styled from 'styled-components';
export default class RenderTableOverHead extends React.Component{
constructor(props) {
super(props);
}
shouldComponentUpdate(nextProps, nextState) {
console.log('shoulItdUpdate');
return false;
}
render(){
console.log('render overhead');
const wrapstyle = 'd-flex align-items-center justify-content-center border-none'
const Button = {
// lots of defined buttons
}
return(
<div className = {wrapstyle}>
{!this.props.isHiddenReturnButton && <Button.ReturnToPreliminarySearch />}
{!this.props.isHiddenWelcomeButton && <Button.Welcome />}
{!this.props.isHiddenFailureMsg && <Button.SearchFailure />}
{!this.props.isHiddenResultsMsg && <Button.SearchResults /> }
{(this.props.isConnectionOK >0||this.props.isConnectionOK===undefined) && <Button.ConnectionError /> }
{!this.props.isHiddenInputForm && <GoogleSearchForm FetchBooks = {this.props.FetchBooks} /> }
{!this.props.isHiddenFilterToggleButton && <Button.FilterShowPrompt />}
</div>
)}}
根据要求 - 由父母这样调用:
import RenderTableOverHead from './components/RenderTableOverHead.js';
在它下面挤满了props以形成新的功能
const Overhead = ()=>{return(
<RenderTableOverHead
isHiddenInputForm={ this.state.isHiddenInputForm}
isHiddenWelcomeButton={this.state.isHiddenWelcomeButton}
ShowGoogleSearch={this.handleWelcomeButtonClick}
//SubmitInputDataToParentState={this.fetchBooks}
FetchBooks ={this.fetchBooks}
isHiddenResultsMsg={this.state.isHiddenResultsMsg}
isHiddenFailureMsg={this.state.isHiddenFailureMsg}
toggleFilterVisibility={this.toggleFilterVisibility}
numberOfResults ={catalog.TotalNumberOfBooks}
numberOfPages =/*{_.last(TableWithPageNumbers)}*/{catalog.NumberOfPages}
currentPageNumber ={catalog.CurrentPageNumber}
//currentPage ={this.Page}//tu podmieniony numer strony jest kosztem powyższego
isConnectionOK={this.state.connectionStatus}
//errorStatus ={this.state.errorStatus}
//errorMessage ={this.state.errorMessage}
//handleError={this.handleError}
isHiddenReturnButton={this.state.isHiddenReturnButton}
isHiddenFilterToggleButton={this.state.isHiddenFilterToggleButton}
/>
);}
并像这样返回(这里它命名为开销)
return(
<div className='container'>
<Overhead />
<Pagination />
{isDataLoaded &&
<div className = {cardStyle}>
<table className = {tableStyle}>
<thead className = {tableHeaderStyle}>
<Headline />
{Filtration}
</thead>
<TableBody />
</table>
</div>};
</div>)