react强制组件刷新
Note: As of React 16, componentWillReceiveProps()
is deprecated, meaning if you are using that version or higher in your project, this is not good advice for you.
注意:从React 16开始, componentWillReceiveProps()
已被弃用,这意味着如果您在项目中使用该版本或更高版本,这对您来说不是一个好建议。
In the React world, forcing a re-render is frowned upon. You should let the DOM take care of itself when React perceives changes to state
or props
. In order to follow these patterns, we sometimes have to do stuff that seems a little silly. Consider this scenario:
在React世界中,强制重新渲染是不受欢迎的。 当React感知到state
或props
变化时,您应该让DOM照顾自己。 为了遵循这些模式,有时我们不得不做一些看起来有些愚蠢的事情。 考虑这种情况:
We’ve got two components — a parent and a child. The parent makes an API call to fetch the user
. From that, we get things like name
, age
, favorite color
. We also get an id
from our database. We’ll pass that to our child component, which is also going to make an API call, with the user id. Awesome — lots of data coming into our app.
我们有两个部分-父母和孩子。 父级进行API调用以获取user
。 由此,我们得到诸如name
, age
, favorite color
。 我们还从数据库中获取了一个id
。 我们会将其传递给子组件,该子组件还将使用用户ID进行API调用。 很棒–大量数据进入了我们的应用程序。
Let’s say we are storing a list of shoes in the database. When the user changes their color preference, the server writes new data to the user’s shoe list. Great! Except, we aren’t seeing the new shoe list i