当前位置: 首页 > 知识库问答 >
问题:

如何在子组件的componentdidmount()中获取道具

陆海阳
2023-03-14

我试图将一个参数从父组件传递给子组件的ComponentDidMount()方法。我只能在子组件的render()中接收道具,而无法将其传递给ComponentDidMount()方法。

父组件-提供程序。js

  export default class Provider extends Component {
      constructor(props) {
        super(props);
        this.state = {
          carePlan: "",
          patID: ""

        };
      }

      async componentDidMount() {
        this.setState({
          carePlan: this.cp
          patID: this.props.location.state.id
        });
        console.log(this.state.patID);
      }

      render() {
        return (
          <div>
            <Layout>

                  {!this.state.cp ? (
                    <AddCarePlan patID={this.state.patID} />
                  ) : (
                    <div className="carePlan">
                      <DisplayCarePlan cp={this.state.carePlan} />
                    </div>
                  )}

              </Content>
            </Layout>
          </div>
        );
      }
    }

子组件-AddCarePlan。js

class AddCarePlan extends Component {
  constructor(props) {
    super(props);
  }

  async componentDidMount() {
    const patientID = this.props.patID;
    console.log(patientID) // does not show ID
  }



  render() {
    const patientID = this.props.patID;
    console.log(patientID) // shows ID
    return (
      <div>
      <h1> Add Care Plan </h1>
      </div>
    );
  }
}

export default AddCarePlan;

共有3个答案

季城
2023-03-14
 export default class Provider extends Component {
  constructor(props) {
    super(props);
    this.state = {
      carePlan: "",
      patID: props.location.state.id

    };
  }

  async componentDidMount() {
    this.setState({
      carePlan: this.cp
    });
    console.log(this.state.patID);
  }

  render() {
    return (
      <div>
        <Layout>

              {!this.state.cp ? (
                <AddCarePlan patID={this.state.patID} />
              ) : (
                <div className="carePlan">
                  <DisplayCarePlan cp={this.state.carePlan} />
                </div>
              )}

          </Content>
        </Layout>
      </div>
    );
  }
}

这样试试

  this.state = {
      carePlan: "",
      patID: ""

    };

  async componentWillMount() {
    this.setState({
      patID: this.props.location.state.id
    });
  }

或者尝试改变生命周期

邹宏峻
2023-03-14

试试这个怎么样?

{!this.state.cp ? (
                    this.state.patID ? <AddCarePlan patID={this.state.patID} /> : ''
                  ) : (
                    <div className="carePlan">
                      <DisplayCarePlan cp={this.state.carePlan} />
                    </div>
                  )}
壤驷康裕
2023-03-14

您应该在组件的生命周期方法之前删除关键字异步。正如我可以告诉你的,没有使用它们内部的等待,反应也不是为了使用异步等待功能而设计的。即使你想使用组件获取数据,你也不应该使用异步,因为当数据到达时,数据获取的()方法会触发组件重渲染。

尝试从代码中删除async。

 类似资料:
  • 我有一个父组件从API获取一些数据,并将数据存储在状态中,然后将其传递给子组件,如下所示: 在我的子组件中,我有两个问题,一个是:我想在componentDidMount中接收一个道具(ratingsAverage),但在render()函数中接收ratingsAverage时未定义。另一个是:在我的render()函数中,我只能接收非对象或数组的数据,当我接收到一个对象并希望访问该对象的属性时,

  • 我有一个要导入页面的组件: 在页面上,我呈现组件

  • 我是新来的,对一些事情有点困惑。我在网上读了许多文章,声称组件不能改变自己的道具,但是父组件可以改变其子组件的道具。然而,我没有看到任何真正展示如何做到这一点的东西。 我希望能够做到这一点: 然而,我根本不知道如何做到这一点——尽管我在React上读到的几乎所有材料都说父母可以改变孩子的道具。我能正常工作的如下: 当我只想重新渲染子视图时,父视图需要重新渲染,这对我来说似乎有点过分了。如何更改<代

  • 问题内容: 我正在使用pyside(qt)开发一个桌面应用程序,我想访问(迭代)QWidget的所有行编辑组件。在qt中,我找到了两个方法 findChild 和 findChildren, 但是没有找到合适的示例,并且我的代码显示错误, “ form”对象没有属性“ findChild”。 这里的“表单”是Qwidget表单,由lineEdit,组合框,Qpushbuttons等组件组成。 码:

  • 如何在Children内部拿到父级的ref? 父级是不同的组件

  • 我创建了一个日历应用程序,可以添加注释。为了实现添加注释的功能,我创建了一些父组件,它有自己的状态,然后传递给子组件。子组件应该在构造函数中接受来自执行的。但是,由于setState函数异步ChildComponent没有时间等待父母组件的道具。 如何设置ChildComponent的初始状态,等待父母组件的道具(换句话说,同步)? 父组件: ChildComponent: