函數(shù)組件與類組件
函數(shù)組件(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>;
}
}
類組件與函數(shù)組件的區(qū)別
- 類組件有state
函數(shù)組件是stateless的宙刘,類組件是stateful的 - 類組件有生命周期函數(shù)
使用
- 函數(shù)組件寫法比較簡單,對于不需要state和生命周期函數(shù)調(diào)用的組件可使用函數(shù)組件列另。React未來也會對函數(shù)組件優(yōu)化敢会,提高其渲染性能纤虽。
- 對比較復(fù)雜的組件還是要使用類組件