您如何将观察者子组件编写为类组件?

问题描述 投票:0回答:1

对于我要实现的目标,我需要能够在子组件中运行componentDidMount()。如何将const Child = observer(({ doc }) => {重写为类组件?

我正在从Firebase中提取数据,并将该数据迭代到子组件中。假设一切正常,我只想重写子组件。

如果我的逻辑有缺陷,请告诉我。

const Parent = observer(
  class Parent extends Component {
    render() {
      return(
          <View>
            {latestEP.docs.map(doc => (
              <Child key={doc.id} doc={doc} ref={this.load} />
            ))}
          </View>
      )
    }
  }
)

const Child = observer(({ doc }) => {
  const { name, id, date, url, description } = doc.data;

  return (
    //some stuff
  );
});
reactjs react-native mobx mobx-react
1个回答
0
投票

您可以像对Child组件所做的那样将Parent组件转换为类,并确保从doc获得了this.props道具。

const Child = observer(
  class Child extends Component {
    componentDidMount() {
      // ...
    }

    render() {
      const { name, id, date, url, description } = this.props.doc.data;

      return (
        // some stuff
      );
    }
  }
);
© www.soinside.com 2019 - 2024. All rights reserved.