Javascript 访问子流中的对象属性
我有一个将道具传递到子组件的父组件。当我试图通过道具a名称或道具a化身访问道具时。我从flow收到以下错误: 无法对未知类型的属性访问属性名称属性 无法对可能未定义的值访问属性名称属性 这是父母的部分Javascript 访问子流中的对象属性,javascript,reactjs,flowtype,Javascript,Reactjs,Flowtype,我有一个将道具传递到子组件的父组件。当我试图通过道具a名称或道具a化身访问道具时。我从flow收到以下错误: 无法对未知类型的属性访问属性名称属性 无法对可能未定义的值访问属性名称属性 这是父母的部分 const items = (authors, authorId) => { const author = authors.find(a => a.id === authorId) const info = { name: author ? author.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>
}
尝试:
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
}
这是因为需要从props对象中提取作者和作者