- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 PureComponents 时与功能组件相比,您的优势在于组件在父组件更新时并不总是呈现。它实际上仅在组件 Prop 发生变化时才会呈现,在此示例中仅当您更改输入时。
如何在不破坏使用此类组件的所有优势的情况下将对象传递给 PureComponent?
这是否意味着一旦你有了一个你希望它是对象类型的 Prop ,最好让你的组件发挥作用?
我添加了一个例子来说明我的意思...(可能你需要在新窗口中打开它,因为有很多不同的 child )
class TestPureComponent extends React.PureComponent {
render() {
return <div style={{border: '1px solid black'}}>{this.props.text} : {this.props.inputValue} {Math.random()} <button onClick={() => {this.props.dismissClicked()}}>Click me</button> </div>
}
}
function TestFunctionalComponent () {
return <div style={{border: '1px solid black'}}>I always update as I am a functional component {Math.random()}</div>
}
const hell = () => {console.log('Logs hello')}
class RenderExample extends React.Component {
constructor (props) {
super(props)
this.state = {clicked: false, inputValue: 'inputValue'}
this.onClick = this.onClick.bind(this)
this.doSomething = this.doSomething.bind(this)
}
onClick () {
this.setState({clicked: !this.state.clicked})
}
doSomething () {
console.log('helllllo')
}
heee = () => {
console.log('heeeee')
}
render () {
return <div>
<button onClick={this.onClick}>
Update state (forces re-render) {this.state.clicked && 'clicked'}
</button>
<input onChange={(e) => this.setState({inputValue: e.target.value})} type="text" value={this.state.inputValue}/>
<br/>
<br/>
<TestFunctionalComponent />
<br/>
<TestPureComponent dismissClicked={() => hell} inputValue={this.state.inputValue} text="If you click the button this will re-render, if you change the input this will re-render"/>
<br/>
<TestPureComponent text="If you click the button this will NOT re-render, if you change the input this will re-render" dismissClicked={this.doSomething} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will NOT re-render, if you change the input this will re-render" dismissClicked={this.heee} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will NOT re-render, if you change the input this will re-render" dismissClicked={hell} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will NOT re-render, if you change the input this will re-render" dismissClicked={hell} inputValue={this.state.inputValue}/>
<br/><br/>
<div> we will now add an inline object to each component and now they all update</div>
<TestPureComponent dismissClicked={() => hell} inlineOBJ={{hello: 'world'}} inputValue={this.state.inputValue} text="If you click the button this will re-render, if you change the input this will re-render"/>
<br/>
<TestPureComponent text="If you click the button this will re-render, if you change the input this will re-render" inlineOBJ={{hello: 'world'}} dismissClicked={this.doSomething} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will re-render, if you change the input this will re-render" inlineOBJ={{hello: 'world'}} dismissClicked={this.heee} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will re-render, if you change the input this will re-render" inlineOBJ={{hello: 'world'}} dismissClicked={hell} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will re-render, if you change the input this will re-render" inlineOBJ={{hello: 'world'}} dismissClicked={hell} inputValue={this.state.inputValue}/>
</div>
}
}
ReactDOM.render(<RenderExample />, document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
我想要的是看看是否有一种 PureComponent 的方法,当有一个 OBJECT 作为 prop 传入时,它不会总是更新。
最佳答案
您必须将该对象存储在某处,以便它不会在每次重新渲染时生成。这也适用于在渲染期间生成的函数(dismissClicked={() => hell})。
函数应该在渲染函数之外,这样它们就不会像这样为每个渲染创建: dismissClicked={this.hell}
对于对象,只需将其保存在状态中即可。
您可以使用功能组件实现完全相同的效果。使用 memo 包裹组件以启用浅比较。
要防止在每次渲染时生成函数,请使用 useCallback 和 useState 来保存您传递给子级的对象,以便引用保持不变。
您可以使用 useEffect 轻松更新这些对象。
希望这对您有所帮助。快乐编码。
关于javascript - 传递对象时防止在 PureComponent 上重新渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57705867/
在 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 . 去年关闭。 社区去年审查了是否重新开放此问题,并将其关
我是一名优秀的程序员,十分优秀!