Open at7211 opened 5 years ago
小寫開頭 => DOM tags 大寫開頭 => component(元件)
Note: Always start component names with a capital letter.
React treats components starting with lowercase letters as DOM tags.
For example, <div /> represents an HTML div tag, but <Welcome /> represents a component and requires Welcome to be in scope.
function formatDate(date) {
return date.toLocaleDateString();
}
function Avatar(props) {
return (
<img
className="Avatar"
src={props.user.avatarUrl}
alt={props.user.name}
/>
);
}
function UserInfo(props) {
return (
<div className="UserInfo">
<Avatar user={props.user} />
<div className="UserInfo-name">{props.user.name}</div>
</div>
);
}
function Comment(props) {
return (
<div className="Comment">
<UserInfo user={props.author} />
<div className="Comment-text">{props.text}</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}
const comment = {
date: new Date(),
text: 'I hope you enjoy learning React!',
author: {
name: 'Hello Kitty',
avatarUrl: 'https://placekitten.com/g/64/64',
},
};
ReactDOM.render(
<Comment
date={comment.date}
text={comment.text}
author={comment.author}
/>,
document.getElementById('root')
);
React最重要的觀念 你的state會永遠對應到你的UI。你每次的任何改變(改變你的資料-state),畫面會跟著變