React进阶

1、React的生命周期钩子

1.1、生命周期钩子

生命周期钩子函数会在组件的生命周期的关键地方自动执行,React提供了以下声明周期钩子:

  • componentWillMount(): 组件加载前调用

  • componentDidMount(): 组件加载后调用

  • componentWillUpdate(): 组件更新前调用

  • componentDidUpdate(): 组件更新后调用

  • componentWillUnmount(): 组件卸载前调用

  • componentWillReceiveProps() : 组件接受新的Props前调用

  • shouldComponentUpdate(): 用于让React知道当前状态或者属性的改变是否影响组件的输出。

1.2、完整生命周期方法

装配
  • constructor():构造函数,装配之前被调用
  • componentWillMount()
  • render():用来描述UI
  • componentDidMount()
更新
  • componentWillReceiveProps()
  • shouldComponentUpdate()
  • componentWillUpdate()
  • render()
  • componentDidUpdate()
卸载
  • componentWillUnmount()

2、Ajax请求

React中,Ajax请求一般在componentDidMount()生命周期钩子中发出,也就是在组件完成加载后。

componentDidMount() {
    const url='...';
    $.getJSON(url)
     .done()
     .fail();
}

参考资料

  1. React 技术栈
  2. 2.

二级标题

三级标题

四级标题

注:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值