简体   繁体   English

尝试从子级更改父级 state 时,父级 State 未更新。 (将处理程序作为道具从父母传递给孩子)

[英]Parent State is not being updated when trying to change the parent state from child. (passing a handler as prop from parent to child)

I know that to change the state of a parent from child we need to pass a handler from parent to child.我知道要从孩子更改父母的 state,我们需要将处理程序从父母传递给孩子。 I am trying to change the state of a parent from child but for some reason the state of parent is not updating.我正在尝试从孩子更改父母的 state 但由于某种原因,父母的 state 没有更新。 Here is the below code这是下面的代码

https://codesandbox.io/s/hungry-raman-6jiwz?fontsize=14 . https://codesandbox.io/s/hungry-raman-6jiwz?fontsize=14

Basically when I click on each name in the table, https://ant.design/components/drawer/ has to pop up if the corresponding status is 'Draft' else just the name.基本上,当我单击表格中的每个名称时,如果相应的状态为“草稿”,则必须弹出https://ant.design/components/drawer/否则只有名称。 After the drawer is popped up, if I close the drawer then I am trying to change the state of the parent with弹出抽屉后,如果我关闭抽屉,那么我试图用

id:''
visible:false

for the drawer to be closed, but for some reason the state of parent is not being updated and for this reason after closing the drawer table is showing the name to be empty.关闭抽屉,但由于某种原因,父级的 state 未更新,因此关闭抽屉表后显示名称为空。

My requirement is to show the name the table after the drawer is closed.我的要求是在抽屉关闭后显示桌子的名称。

Any help would be appreciated...任何帮助,将不胜感激...

Your turnery opperator is inncorrect您的车工操作员不正确

Change line the first entry in the column array to this将列数组中的第一个条目更改为此

{
  title: "Name",
  dataIndex: "name",
  key: "name",
  render: (text, i) => (
    <a
       onClick={() => {
         this.setState({ id: i.key });
       }}
    >
     {this.state.id == i.key && i.status == "Draft" && <Test reset={this.reset} text={text} />}
     {text}
    </a>
  )
},

You had the text variable or the popup but you need the text variable and the popup.您有文本变量或弹出窗口,但您需要文本变量和弹出窗口。

So instead of boolean? truthy value: falsey value所以代替boolean? truthy value: falsey value boolean? truthy value: falsey value

You need only the truthy boolean && truthy value您只需要boolean && truthy value

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

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