函數組件與類組件
函數組件(functional component)
function Welcome(props){
return <h1>Hello, {props.name}</h1>;
}
類組件(class component)
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
類組件與函數組件的區別
- 類組件有state
函數組件是stateless的,類組件是stateful的 - 類組件有生命周期函數
使用
- 函數組件寫法比較簡單,對于不需要state和生命周期函數調用的組件可使用函數組件。React未來也會對函數組件優化,提高其渲染性能。
- 對比較復雜的組件還是要使用類組件