简体   繁体   English

如何将对象从父类传递给子类

[英]How to pass object from parent to child class

I am trying to pass an object from the parent to the child class.我试图将一个对象从父类传递给子类。 I make a GET request with axis, where I get the object, then I set the response of the request as this.state.contact in my parent class.我使用轴发出 GET 请求,在那里获取对象,然后在父类中将请求的响应设置为this.state.contact Until there all seems good, if I console.log the contact in state it shows up correctly.直到一切看起来都很好,如果我 console.log 状态下的联系人,它会正确显示。

The object looks like this when output in the console: {first_name: "Test", last_name: "surname", email: "test@hotmail.com", phone_number: "7228732837", slug: "testhotmailcom"} and if I console log typeof this.state.contact it prints Object在控制台中输出时,对象看起来像这样: {first_name: "Test", last_name: "surname", email: "test@hotmail.com", phone_number: "7228732837", slug: "testhotmailcom"}如果我控制台日志typeof this.state.contact它打印Object

In my child class I just take this.props.contact assign it to this.state.data and console log it in the render method.在我的子类中,我只是将this.props.contact分配给this.state.data并控制台将其记录在渲染方法中。 However, the console prints undefined .但是,控制台打印undefined

How can I pass this object from the parent class into the child class?如何将此对象从父类传递到子类? I need to set the states in the child class with the props as I will need to render a form with these values.我需要使用道具在子类中设置状态,因为我需要使用这些值呈现表单。

Here are both classes:这是两个类:

Parent class:父类:

 import React from 'react'; import axios from "axios"; import UpdateContact from './UpdateContact'; export default class Test extends React.Component { state = { contact: '' }; componentDidMount() { const slug = this.props.match.params.slug const url = "http://localhost:3000/api/v1/contacts/" + slug + ".json" axios.get(url) .then( (resp) => { const contact = resp.data.data.attributes this.setState({ contact }) }) .catch( data => { debugger }) } render() { return( <div> <UpdateContact contact={this.state.contact}/> </div> ) } }

Child class:儿童班:

 import React from 'react'; export default class UpdateContact extends React.Component { constructor(props){ super(props); this.state = { data: this.props.contact } } render(){ return( <div> {console.log(this.state.data)} </div> ) } }

State initialization is run only once when component is mounted and the props.contact is undefined yet.状态初始化仅在组件已安装且 props.contact 尚未定义时运行一次。 That is why you got undefined in your render.这就是您在渲染中未定义的原因。

You need to add componentDidUpdate lifecycle method if you want to update your child component state based on props如果要根据 props 更新子组件状态,则需要添加componentDidUpdate生命周期方法

  componentDidUpdate(prevProp) {
    if (this.props.contact !== prevProps.contact) {
      this.setState({ data: this.props.contact });
    }
  }

The other thing is that if you don't need to change this.props.contact in your Child component, it's way better to use the prop directly instead of setting state which is not gonna change internally.另一件事是,如果您不需要更改 Child 组件中的this.props.contact ,最好直接使用 prop 而不是设置不会在内部更改的状态。

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

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