简体   繁体   English

在子状态更改时触发父组件的更新

[英]Triggering update of parent component on child state change

I am exploring firebase using React as a frontend framework and got stuck on a following problem: 我正在使用React作为前端框架来探索firebase,并且遇到了以下问题:

I have the following parent component with a lifecycle hook that gets my data from firebase and populates a local state with it. 我有以下父组件与生命周期钩子,从firebase获取我的数据并用它填充本地状态。 After that the component outputs the state . 之后,组件输出状态。

state = {
    pets: null
  };

  componentDidMount() {
    db
      .onceGetPets()
      .then(snapshot => this.setState(() => ({ pets: snapshot.val() })));
  }

  render() {
    const {pets} = this.state;
     return ({pets})}

I also have a child component that writes new entries into the data base and looks the following way: 我还有一个子组件,它将新条目写入数据库并按以下方式查找:

state = {
    ...INITIAL_STATE
  };

  submitHandler = event => {
    const { name, age, animal, breed } = this.state;

    db.doCreatePet(
      name,
      age,
      animal,
      breed,
      auth.currentUser.email.slice(0, -4)
    );
    this.setState({ ...INITIAL_STATE });
    event.preventDefault();
  };
    render(){
      return(<StyledNewPetForm show={this.props.show} onSubmit={this.submitHandler}/>)}

I want to achieve the following behavior: 我想实现以下行为:

When user submits the form(child component) it should trigger rerender of parent component(the one that displays data) that would in turn get new data from firebase and display it. 当用户提交表单(子组件)时,它应该触发重新呈现父组件(显示数据的组件),然后从firebase获取新数据并显示它。

Instead of calling the pets data in componentDidMount, move it to separate function like below and bind the function in the constructor. 而不是在componentDidMount中调用宠物数据,而是将其移动到如下所示的单独函数并绑定构造函数中的函数。

fetchPetData(){
 db
      .onceGetPets()
      .then(snapshot => this.setState(() => ({ pets: snapshot.val() })));
}

and call the function in componentDidMount as below 并在componentDidMount中调用该函数,如下所示

componentDidMount() {
    this.fetchPetData();
  }

Then you can pass the function as props to child component and call the function in child component in submit function as below. 然后你可以将函数作为props传递给子组件,并在提交函数中调用子组件中的函数,如下所示。

this.props.fetchPetData();

and in the parent component 并在父组件中

<Child fetchPetdata={this.fetchPetData} />

A quick solution would be passing a function prop to the parent component which you'd call on update. 一个快速的解决方案是将函数prop传递给您在更新时调用的父组件。 So in your child function you have something like 所以在你的孩子功能中,你有类似的东西

submitHandler = event => {
  const { name, age, animal, breed } = this.state;

  db.doCreatePet(
    name,
    age,
    animal,
    breed,
    auth.currentUser.email.slice(0, -4)
  );
  this.setState({ ...INITIAL_STATE });
  event.preventDefault();
  this.props.reloadData(); // Add this
};

while in the parent component's render function you'd have 而在父组件的渲染功能中你会有

fetchData() {
  db
    .onceGetPets()
    .then(snapshot => this.setState(() => ({ pets: snapshot.val() })));
}

and in the render method you'd 在你的渲染方法中

render() {
  return (
    <Child reloadData={this.fecthData} ...props/>
  )
}

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

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