react组件分类

本文介绍了React中的三种主要组件类型:有状态组件、无状态组件及高阶组件,并探讨了它们的特点与应用场景。此外,还讨论了双向数据绑定的概念以及生命周期方法UNSAFE_componentWillReceiveProps的作用与使用建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,有状态组件, 有自己的state 在constructor中定义 继承Component基类的方法能够在生命周期内通过setState方法改变state改变Virtul DOM从而从新渲染DOM

    eg: export default class Peoson extends Component{

//oop    

constructor(props){

this.state={}

}

render(){}

}

2.无状态组件 没有定义自己的state 不需要处理用户的输入 组件所有数据依赖props传入 不需要用到生命周期函数

constructor defaultProps componentWillMount render componentDidMount

不需要声明类 避免大量extends class constructor

不需要显示声明this 关键字  es6 的class写法需要把this绑定到当前作用域  

用来定义无副作用的纯函数

const Header =( props) => {

return (
< div className= "container" >
< div className= "row" >
< div className= "col-xs-1 col-xs-offset-11" >
< h1 >Header </ h1 >
</ div >
</ div >
</ div >
);
}
export default Header

3.高阶组件

返回组件的组件(函数)

4.双向数据绑定

state(model)  view  (v)

5.生命周期

UNSAFE_componentWillReceiveProps()

UNSAFE_componentWillReceiveProps(nextProps)

Note

It is recommended that you use the static getDerivedStateFromProps lifecycle instead of UNSAFE_componentWillReceivePropsLearn more about this recommendation here.

UNSAFE_componentWillReceiveProps() is invoked before a mounted component receives new props. If you need to update the state in response to prop changes (for example, to reset it), you may compare this.props and nextProps and perform state transitions using this.setState() in this method.

Note that if a parent component causes your component to re-render, this method will be called even if p rops have not changed. Make sure to compare the current and next values if you only want to handle changes.

React doesn’t call UNSAFE_componentWillReceiveProps() with initial props during mounting. It only calls this method if some of component’s props may update. Calling this.setState()generally doesn’t trigger UNSAFE_componentWillReceiveProps().

Note

This lifecycle was previously named componentWillReceiveProps. That name will continue to work until version 17. Use the rename-unsafe-lifecycles codemod to automatically update your components.

componentWillReceiveProps 不仅在组件props

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值