简体   繁体   English

从子类刷新父函数小部件

[英]Refresh Parent function widget from child class

I wan to refresh parent component from child class,我想从子类刷新父组件,

I read some articles but still confusing because parent is function and child is class我读了一些文章,但仍然令人困惑,因为 parent 是function而 child 是class

I appreciate your any help thank you very much.我感谢您的任何帮助,非常感谢。

Parent function component父函数组件

export default function HeaderLinks(props) {
  const classes = useStyles();

  return (
      <ChildWidget myHeaderLink={this}></ChildWidget>

Child Class儿童班

class AuthWidget extends React.Component {
  constructor(props) {
    super(props); 
  }
  refreshParent(){
        this.props.myHeaderLink.setState() ..??? maybe I need something equivalent to setState()?

I create a very small sample to display this useCase.我创建了一个非常小的示例来显示这个用例。 You can check it here.你可以在这里检查。 Just pass prop and onClick I handle it on the parent which update the state of parent and eventually that state change leads to the Re-renders of the component.只需通过 prop 和 onClick 我在父级上处理它,更新父级的状态,最终状态变化导致组件的重新渲染。 https://codesandbox.io/s/intelligent-dubinsky-w32dz?file=/src/App.js https://codesandbox.io/s/intelligent-dubinsky-w32dz?file=/src/App.js

Click on Button "Change parent Property" changes the parent state from "Hello World" to "Bye World"单击按钮“更改父属性”将父状态从“Hello World”更改为“Bye World”

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

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