简体   繁体   English

单击后退按钮时重新渲染子组件

[英]Re-render child component when back button is clicked

Im wondering if there is any straightforward way to get a single child component to rerender when the back button is clicked.我想知道是否有任何简单的方法可以在单击后退按钮时重新渲染单个子组件。 For example:例如:

componentWillReceiveProps: function() {
   forceupdate > child-one
}

<parent>
  <child-one />
  <child-two />
</parent>

componentWillReceiveProps is hit whenever the back button is clicked.每当单击后退按钮时,都会点击componentWillReceiveProps

Any ideas?有什么想法吗? I don't want to re-render the entire parent component.我不想重新渲染整个父组件。

If for some reason you want the children to re-render you could use key prop passed to them.如果出于某种原因您希望孩子们重新渲染,您可以使用传递给他们的 key 道具。 This key prop which is used by React Reconciliation will just throw the old component and create a new one, thus re-render. React Reconciliation 使用的这个 key prop 只会抛出旧组件并创建一个新组件,从而重新渲染。

<parent>
  <child-one key={control} />
  <child-two />
</parent>

Sources: https://reactjs.org/docs/reconciliation.html#keys来源: https : //reactjs.org/docs/reconciliation.html#keys

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

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