创建第一个组件
使用 function Profile() { }
定义名为 Profile
的 JavaScript 函数,组件名称必须是大写
使用 export default 导出
返回语句可以全写在一行上,如下面组件中所示:
return <img src="https://i.imgur.com/MK3eW3As.jpg" alt="Katherine Johnson" />;
但是,如果你的标签和 return
关键字不在同一行,则必须把它包裹在一对括号中,如下所示
return (
<div>
<img src="https://i.imgur.com/MK3eW3As.jpg" alt="Katherine Johnson" />
</div>
);
function Profile() {
return (
<img
src="https://i.imgur.com/MK3eW3As.jpg"
alt="Katherine Johnson"
/>
);
}
export default Profile
使用组件
export default function Gallery() {
return (
<section>
<h1>了不起的科学家</h1>
<Profile />
</section>
);
}