- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们使用 ReactJS 创建了一个可重用的列表组件。然而,由于性能问题,我们决定实现 - shouldComponentUpdate 方法,其条件是我的列表组件何时渲染
public shouldComponentUpdate(nextProps: TreeItemInternalProps, nextState: {}): boolean {
if (this.props.treeObject.selected !== nextProps.treeObject.selected) {
return true;
}
if (this.props.treeObject.expanded !== nextProps.treeObject.expanded) {
return true;
}
return false;
}
意思是,我只想在列表项复选框的值发生变化时渲染我的组件。
假设,由于某种原因,我不能再这样做了。现在,我的替代方案是使用 PureComponent 进行浅层比较。因此,它应该只渲染发生更改的列表项。然而,即使在使用 PureComponent 之后使用,整个组件也会被渲染。
为了正确解释该场景,请参阅以下屏幕截图 -
在这里,您可以看到日志“组件已渲染”仅被调用一次,这意味着,仅针对我在 Prop 中所做的更改(这是相同的要求,我想使用 PureComponent 来实现)
<小时/>最佳答案
PureComponent 始终为您渲染,因为您的 Props
或 State
很可能包含在父组件重新渲染期间在父组件中创建的任何对象或数组。
正如其他人所说,它对重新渲染进行浅层比较(意味着对象引用不会等于新对象引用,即使它们深度相等),因此父组件充当了它的瓶颈PureComponent 子级,因为每次重新渲染都会重新创建一个全新的对象引用
如果您真的想使用 PureComponent,则必须将组件中的所有其他 props 简化为基元或对象/数组,这样在每次重新渲染父级时都不会获得新的引用
问题示例:
class Todo extends PureComponent {
render() {
return <div>this.props.foo</div>;
}
}
class MyTodoList extends Component {
render () {
const fooBar = {foo: 'bar'}
return <Todo fooBar={fooBar} />
}
}
修复示例:
class Todo extends PureComponent {
render() {
return <div>this.props.foo</div>;
}
}
class MyTodoList extends Component {
render () {
return <Todo fooBar={props.fooBar} />
}
}
您想要做的最好的事情可能是尽可能提高对象创建的程度,使组件不会在此类更改上重新渲染,或者将每个 Prop 简化为非对象。
关于javascript - React.PureComponent 渲染列表组件中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980174/
在 redux 中,如果您的 action/reducer 更新了 foo.bar 的值和您连接的组件的 mapStateToProps 是 (store) => { foo: store.foo }
我正在使用 FlatList,在我的 renderItem 中我有一个名为 GridItem 的 PureComponent。我正在尝试传递需要 item 参数的函数 wishlistAddPrese
我的理解是 PureComponent 利用 shouldComponentUpdate() 并对状态和 props 进行浅层比较,但我创建了一个运行方式与我预期不同的小示例。 示例应用程序显示人员列
考虑以下 React 代码: class Todos extends React.Component { constructor(props) { super(props); th
有人请用一个简单的例子向我解释 React 文档的最后一段,它是关于 React.PureComponent 的,我见过的所有例子都是高级的,我刚刚开始了解这个概念,我看不出它到底是什么指。 正是 c
我想知道这是否可以: import React, { PureComponent } from "react"; import { Text, TouchableOpacity } from "rea
我们使用 ReactJS 创建了一个可重用的列表组件。然而,由于性能问题,我们决定实现 - shouldComponentUpdate 方法,其条件是我的列表组件何时渲染 public shouldC
我目前在我的应用程序中使用 PureComponent 父组件和子组件。我预计 PureComponent 仅在状态或 Prop 发生变化时才会更新。为了确保这一点,我让一个子组件在每次调用它的 co
使用 PureComponents 时与功能组件相比,您的优势在于组件在父组件更新时并不总是呈现。它实际上仅在组件 Prop 发生变化时才会呈现,在此示例中仅当您更改输入时。 如何在不破坏使用此类组件
我是 React 新手,我想知道什么时候应该使用 React Component 以及什么时候应该使用 React PureComponent? 组件: import React, { Compone
我可能会遗漏一些东西,但我有一个这样的组件 export default MyComponent extends React.PureComponent { // ... } 当 MyComp
使用 PureComponent 来提高 React 中的渲染性能似乎是一种常见的技术。然而,当使用具有子项作为 props 的 PureComponent 时,情况似乎并非如此。 class App
向此示例添加样式组件后,我们注意到,当仅修改状态中的一项时,我们的列表组件会更新所有内容。 添加/删除单个条目时,列表渲染亮点(来自 React 开发工具)过多。删除/添加一项,然后所有项目都会突出显
React 表示纯渲染可以优化性能。现在 React 有了 PureComponent。我应该在任何地方使用 React.PureComponent 吗?或者什么时候使用React.PureCompo
阅读When to Use Component or PureComponent在不要在渲染中的函数中绑定(bind)值部分中,它提到而不是这样做 this.likeComment(user.id)
根据文档(https://reactjs.org/docs/react-api.html#react.purecomponent) "React.PureComponent is exactly li
我正在尝试创建一个组件,当某个属性为真时,它不应该执行,但应该执行浅比较(PureComponent 的默认设置)。 我尝试过以下行为: export default class ContentsLi
我知道 shouldComponentUpdate 和 PureComponent 的功能。但我想知道我是否可以同时使用这两者? 假设我有很多 Prop ,我想让 PureComponent 中的浅层
我正在使用PureComponent为了在我的 React 应用程序中获得更好的性能,它有 props但我不希望它在 props 时运行渲染方法改变了。我知道我们不能使用shouldComponent
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 去年关闭。 社区去年审查了是否重新开放此问题,并将其关
我是一名优秀的程序员,十分优秀!