简体   繁体   English

如果React组件的属性更新但不改变其价值,它会重新渲染吗?

[英]Will a React component re-render if its props are updated, but don't change in value?

Let's say we've got a redux store that looks like this: 假设我们有一个redux商店,看起来像这样:

{
  "page1": {
    "title": "Demo",
    "content": "Testing"
  },
  "page2": {
    "title": "Demo",
    "content": "Yes, I'm aware the titles match."
  }
}

Now let's look at the component that renders the title: 现在让我们看一下呈现标题的组件:

const Title = ({title}) => <h1>{title}</h1>

const mapStateToProps = state => ({
  title: getCurrentPage().title
})

export default connect(mapStateToProps)(Title)

In the above block, getCurrentPage() is a memoized function (via reselect) that derives the current page data from the state based on some condition (in this case, it's react-router's location.pathname). 在上面的块中,getCurrentPage()是一个记忆化的函数(通过重新选择),该函数根据某种条件(在这种情况下,它是react-router的location.pathname)从状态中导出当前页面数据。

Will this component re-render when the data returned by getCurrentPage() is different, even though the specific value of {title} stays the same? 即使{title}的特定值保持不变,当getCurrentPage()返回的数据不同时,此组件是否也会重新渲染?

If yes, will memoizing the derived title prop change the answer to no? 如果是,则记住派生的标题道具会否定答案?

Ok, I took the time to test out my own question because the first answer I received said it would re-render. 好的,我花时间测试我自己的问题,因为我收到的第一个答案说它将重新呈现。 I inserted console.log('render') into the Title component and then navigated between two separate pages that each shared the same exact title. 我将console.log('render')插入到Title组件中,然后在两个共享相同确切标题的单独页面之间导航。 And the result... (drum roll) 结果……(鼓声)

NO . 不行 The component did not re-render. 组件未重新渲染。

The component will re-render if the props changed, or the local state changed due to a call to setState() , or you explicitly call the foceUpdate() function. 如果通过调用setState()更改道具或更改本地状态,或者您显式调用foceUpdate()函数,则组件将重新渲染。 In your case, the title is a string, and the old string and new string have the same value, so, there is no change in the props, therefore no re-render. 在您的情况下,标题是一个字符串,并且旧字符串和新字符串具有相同的值,因此,道具没有任何变化,因此没有重新渲染。

One thing to be careful is passing an object to a props. 要注意的一件事是将一个物体传递给道具。 react won't do a deep comparison, so the result may be different from your expectation. react不会做深入的比较,因此结果可能与您的预期有所不同。 In that case, you need to compare the old and new objects by yourself in the getDerivedStateFromProps or componentWillRecieveProps in old versions, and then decide whether you need to call forceUpdate() to force the component to re-render. 在这种情况下,您需要在旧版本的getDerivedStateFromPropscomponentWillRecieveProps中自己比较旧对象和新对象,然后确定是否需要调用forceUpdate()来强制重新渲染组件。

in my tests not with redux and object it does and i had add a check comand to my compoments for only updating when real change. 在我的测试中,它没有使用redux和object,并且我在自己的组件中添加了一个检查命令,仅在真正更改时才进行更新。

Btw why not test with a simple console.log? 顺便说一句,为什么不使用简单的console.log进行测试?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM