首页 文章

在Child React Flow中访问对象Prop

提问于
浏览
0

我有一个父组件,它将prop传递给子组件 . 当我尝试通过prop.a.name或prop.a.avatar访问prop时 . 我从流程中收到以下错误:

属性 name 属性 a 未知类型属性 name 无法访问属性可能未定义的值

这是父母的一部分

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
}

现在,如果我返回info.name或info.profileImage,它会传递,但我想将此对象传递给子节点,以便我可以访问子组件中需要的位置 .

这是孩子

type Props = {
  info: string
}

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

1 回答

  • 1

    试试:

    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
    }
    

    这是因为作者和authorId需要从props对象中提取 .

相关问题