简体   繁体   English

子反应流中的访问对象道具

[英]Access Object Prop In Child React Flow

I have a parent component that passes a prop into the child component. 我有一个将组件传递给子组件的父组件。 When I try to access the prop by prop.a.name or prop.a.avatar. 当我尝试通过prop.a.name或prop.a.avatar访问prop时。 I receive the following errors from flow : 我从flow收到以下错误:

property name Property cannot be accessed on property a of unknown type property name Property cannot be accessed on possibly undefined value 属性name无法在未知类型的属性a上访问属性属性name无法在可能未定义的值上访问属性

Here is the part of the parent 这是父母的一部分

const items = (authors, authorId) => {
const author = authors.find(a => a.id === authorId)
  const info = {
   name: author ? author.name : '',
   profileImage: author ? author.profileImage : '',
   company: author ? author.company : '',
 }
  return info
}

Now if I return info.name or info.profileImage it passes but I want to pass this object into the child so I can access where needed in child component. 现在,如果我返回info.name或info.profileImage,它会通过,但我想将此对象传递给子对象,这样我就可以访问子组件中需要的位置。

Here is the child 这是孩子

 type Props = {
  info: string
}

const child = (props: Props) => { 
   ... some code 
   <h1>{props.info.name}</h1> 
}

Try with: 尝试:

const items = ({authors, authorId}) => {
const author = authors.find(a => a.id === authorId)
const info = {
  name: author ? author.name : '',
  profileImage: author ? author.profileImage : '',
  company: author ? author.company : '',
 }
 return info
}

This is beacause authors and authorId need to be extracted from props Object. 这是因为authors和authorId需要从props Object中提取。

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

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