【发布时间】:2019-08-14 16:06:11
【问题描述】:
我在学习 React 时遇到了一个令人困惑的问题。在编写函数组件时,我到处都在使用 props。
我总是使用props.profile,它工作正常。但是在一个代码组件中,我必须编写
const profiles=props; 效果很好。
我尝试使用const profiles=props.profile;我还尝试在“卡片”功能组件中使用 return
{props.profile.avatar_url} 但他们都失败了
下面是我的代码,运行良好
const Card=(props)=>{
const profiles=props; //This I dont understand
return(
<div>
<div>
<img src={profiles.avatar_url} width="75px" alt="profile pic"/>
</div>
<div>
<div>{profiles.name}</div>
<div>{profiles.company}</div>
</div>
</div>
);
}
const CardList=(props)=>{
return(
<div>
{testDataArr.map(profile=><Card {...profile}/>)}
</div>
);
}
谁能帮我理解为什么我不能使用const profiles=props.profile?
还有哪些其他方法可以达到正确的结果?
【问题讨论】:
-
props.avatar_url工作正常吗?然后就做const Card=(profiles)=>{ -
@Federkun 你的意思是 const
Card=({profile})=>{?
标签: javascript reactjs