简体   繁体   English

如何对子组件进行 React forceUpdate()?

[英]How to do React forceUpdate() to a child component?

I can force reload for a parent component with this.forceUpdate() , but how to do it for a child component?我可以使用this.forceUpdate()强制重新加载父组件,但是如何为子组件执行此操作?

ie following react class methods:即以下反应 class 方法:

buttonClick = () => {
   //This updates parent (this) component
   this.forceUpdate(); 

  //This causes runtime error ("..not a function in buttonClick...")
   Rtable.forceUpdate();
}

render () {
  <div>
     <RTable />
  </div>
}

If child component is not memoized then rendering its parent will render the child.如果子组件没有被记忆,那么渲染它的父组件将渲染子组件。

If child component is memoized , you could force update with its reference .如果子组件被记忆,你可以强制更新它的引用

And of course, passing different properties to a child, changing its state, will re-render it.当然,将不同的属性传递给一个孩子,改变它的 state,会重新渲染它。

Changing the key will UNMOUNT the component, it is like making a conditional rendering on it.更改密钥卸载组件,就像在其上进行条件渲染一样。

Here is an example demonstating everything mentioned:这是一个演示所有提到的示例:

class Child extends React.Component {
  componentDidMount() {
    console.log("mounted");
  }
  render() {
    console.log("rendered");
    return <div>Child</div>;
  }
}

class App extends React.Component {
  state = {
    counter: 0
  };

  childRef = React.createRef();
  onClick = () => this.forceUpdate();
  onCounter = () => this.setState({ counter: this.state.counter + 1 });
  onRef = () => this.childRef.current.forceUpdate();

  render() {
    return (
      <>
        <Child key={this.state.counter} ref={this.childRef} />
        <button onClick={this.onClick}>Render</button>
        <button onClick={this.onCounter}>Update Key</button>
        <button onClick={this.onRef}>Render with ref</button>
      </>
    );
  }
}

编辑 lucid-hill-vfvr3

<RTable key={some variable that changes between renders} />

As pointed out below this will cause unmount, which causes the child to lose its state and also run componentWillUnmount , componentDidMount and constructor methods (which may be useful when you want to resubscribe).正如下面所指出的,这将导致卸载,这会导致子级丢失其 state 并且还会运行componentWillUnmountcomponentDidMountconstructor方法(当您想要重新订阅时可能很有用)。

Documentation https://reactjs.org/docs/glossary.html#keys文档https://reactjs.org/docs/glossary.html#keys

暂无
暂无

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

相关问题 如何在没有forceupdate的情况下让反应组件重新渲染? - How to get a react component to rerender without forceupdate? 如何在render()的JSX组件树中定义的组件上调用React forceUpdate() - How to call React forceUpdate() on the component that is defined in JSX component tree in render() 如何解决:无法在 React-Native 中的未挂载组件警告上调用 setState(或 forceUpdate)? - How to solve: Can't call setState(or forceUpdate) on an unmounted component warning in React-Native? 如何强制更新(重新渲染)React 组件? - How to forceUpdate (re-render) React components? 如何使this.forceUpdate()应用于React Native中的整个应用程序? - How do you make this.forceUpdate() apply to entire app in React Native? 反应警告:无法在未安装的组件上调用 setState(或 forceUpdate) - React Warning: Can't call setState (or forceUpdate) on an unmounted component 警告:无法在React Native中的已卸载组件上调用setState(或forceUpdate) - Warning: Can't call setState (or forceUpdate) on an unmounted component in React Native 反应-警告:无法在未安装的组件上调用setState(或forceUpdate) - React - Warning: Can't call setState (or forceUpdate) on an unmounted component 无法在已卸载的组件上调用setState(或forceUpdate)。 应对 - Can't call setState (or forceUpdate) on an unmounted component. React 你如何从 React 中的子组件状态聚合价值? - How do you aggregate value from child component states in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM