react-- 常见的组件优化
ShouldComponentUpdate
渲染前会进行比较,如果返回的react元素和之前的想用就不需要render,可以看下面的例子
正常情况下:
import React, { Component } from "react";
export default class ShouldComponentUpdate extends Component {
constructor(props) {
super(props);
this.state = {
comments: [],
};
}
componentDidMount() {
setInterval(() => {
this.setState({
comments: [
{
author: "小明",
body: "这是小明写的文章",
},
{
author: "小红",
body: "这是小红写的文章",
},
],
});
}, 1000);
}
render() {
const { comments } = this.state;
return (
<div>
<h1>CommentList</h1>
{comments.map((item) => {
return <Comment key={item.author} data={item} />;
})}
</div>
);
}
}
class Comment extends Component {
render() {
const { author, body } = this.props.data;
console.log("render");
return (
<div className="border">
<p>作者: {author}</p>
<p>内容: {body}</p>
</div>
);
}
}
可以看到一直在render
使用ShouldComponentUpdate后
import React, { Component } from "react";
export default class ShouldComponentUpdate extends Component {
constructor(props) {
super(props);
this.state = {
comments: [],
};
}
componentDidMount() {
setInterval(() => {
this.setState({
comments: [
{
author: "小明",
body: "这是小明写的文章",
},
{
author: "小红",
body: "这是小红写的文章",
},
],
});
}, 1000);
}
render() {
const { comments } = this.state;
return (
<div>
<h1>CommentList</h1>
{comments.map((item) => {
return <Comment key={item.author} data={item} />;
})}
</div>
);
}
}
class Comment extends Component {
shouldComponentUpdate(nextProps, nextState) {
const { author, body } = nextProps;
const { author: nowAuthor, body: nowBody } = this.props;
if (nowAuthor === author && nowBody === body) {
// 减少reader
return false;
}
return true;
}
render() {
const { author, body } = this.props.data;
console.log("render");
return (
<div className="border">
<p>作者: {author}</p>
<p>内容: {body}</p>
</div>
);
}
}
可以看到只render了2次
PureComponent
import React, { Component, memo, PureComponent } from "react";
export default class MemoPage extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0,
obj: {
num: -1,
},
};
}
setCounter = () => {
this.setState({
counter: 1,
});
};
render() {
const { counter, obj } = this.state;
return (
<div>
<h1>PureComponentPage</h1>
<button onClick={this.setCounter}>change</button>
<Demo counter={counter} obj={obj} />
</div>
);
}
}
class Demo extends PureComponent {
render() {
const { counter } = this.props;
console.log("render");
return <div>{counter}</div>;
}
}
当点击多次的时候只会执行一次,Component 会执行多次
const Demo = memo(props => { const { counter } = props;
console.log("render");
return <div>{counter}</div>;
});
和PureComponent 一样的效果。
缺点
只会进行浅比较
请用今天的努力,让明天没有遗憾。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器