React 生命周期
在React
中,生命周期函数是指 组件在某一时刻会自动执行的函数
组件的生命周期官方文档
生命周期图谱
生命周期函数都是对于组件的。
挂载
-更新
-卸载
使用示例
axios
安装 axios npm文档
yarn add axios
或
npm install axios --save
// hello.js
import React from 'react'
import axios from 'axios'
class Hello extends React.Component {
handleClick () {
console.log('hello click')
}
componentWillMount () {
// 现在已经弃用了
window.addEventListener('click', this.handleClick)
}
render () {
return (
<div onClick={this.handleClick.bind(this)}>hello world</div>
)
}
componentWillUnmount () {
window.removeEventListener('clickremouve', this.handleClick)
}
componentDidMount () {
// 发送请求
const promise = axios.get('http://www.dell-lee.com/react/api/demo.json')
promise.then(res => {
console.log(res)
})
}
}
export default Hello