什么是无状态组件?
- 不依赖他自身的状态state。
- 可以是类组件或者函数组件。
- 可以完全避免使用
this
关键字。(由于使用的是箭头函数事件无需绑定) - 有更高的性能。当不需要使用生命周期钩子时,应该首先使用无状态函数组件
什么时候用?
组件不需要管理 state 只是纯的展示时
无状态组件没有 refs 属性
示例
无状态组件
const Component (props)=>(
<div>
{props.text}
...
</div>
)
有状态组件
export default class CusImg extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div className={this.props.style}>
<img src={this.props.imgurl}/>
<text className={this.props.textStyle}>{this.props.text}</text>
</div>);
}
}