简体   繁体   English

道具更改时渲染反应组件

[英]render react component when prop changes

how to force render our component when props changes? 道具更改时如何强制渲染组件? how to force render parent component when child component's props changes? 子组件的props更改时如何强制渲染父组件?

i searched a lot but all solutions are deprecated in new React version. 我搜索了很多,但是所有解决方案在新的React版本中都已弃用。

in my any pages (Route exact path="/post/:id" component={post}) for example (siteUrl/post/1) i am getting (:id) from props (this.props.match.params) and that works. 在我的任何页面中(路由精确路径=“ / post /:id” component = {post}),例如(siteUrl / post / 1),我从道具(this.props.match.params)获取(:id)并这样可行。

but when i am in single page component (Post) and this Route (siteUrl/post/1) when i am passing new Props to this component (:id). 但是当我在单页组件(Post)和此路由(siteUrl / post / 1)中时,我将新的Props传递到该组件(:id)。

props will changes but single component and parent component Will not re render... 道具会发生变化,但单个组件和父组件不会重新渲染...

To make both parent and child re-render you need to path prop from parent to it's child. 要重新渲染父级和子级,您需要将prop从父级传递到其子级。

   // parent using
   <Parent someProp={{someVal}} />

   // parent render:

   render() {
      const { someProp } = this.props
      <Child someProp={{someProp}} />
   }

this will surely re-render both components, unless you stated another logic in componentShouldUpdate 这肯定会重新渲染两个组件,除非您在componentShouldUpdate声明了另一个逻辑

in your case Router looks like a parent for Parent so you should only path :id as a prop. 在您的情况下, Router看起来像父级的Parent因此您仅应将路径:id作为道具。

Make sure Router is at the top level, right under the App 确保Router位于App正下方的顶层

You may be using componentDidMount method. 您可能正在使用componentDidMount方法。

componentDidMount() is invoked immediately after a component is mounted (inserted into the tree). 挂载组件(插入树中)后立即调用componentDidMount()。 Initialization that requires DOM nodes should go here. 需要DOM节点的初始化应在此处进行。 If you need to load data from a remote endpoint, this is a good place to instantiate the network request. 如果需要从远程端点加载数据,这是实例化网络请求的好地方。

but you need to use componentDidUpdate. 但是您需要使用componentDidUpdate。

componentDidUpdate() is invoked immediately after updating occurs. 发生更新后,立即调用componentDidUpdate()。 This method is not called for the initial render. 初始渲染不调用此方法。

You can also use state and other React features without writing a class. 您也可以使用状态和其他React功能,而无需编写类。 read more: https://reactjs.org/docs/hooks-effect.html 了解更多: https//reactjs.org/docs/hooks-effect.html

Important is ,that you initialise the someVal of the child first in the constructor 重要的是,您首先要在构造函数中初始化子项的someVal

 public static getDerivedStateFromProps(
        nextProps,
        nextState
      ) {
        let { someVal } = nextProps;
        if (nextState.someVal !== someVal) {
          return {
            initialVal,
            someVal: someVal
          };
        }
        return null;
      }

After it will rerender on prop changes because the state changes 之后,由于状态更改,将在属性更改时重新呈现

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

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