区别
区别 | 函数组件 | 类组件 |
---|---|---|
生命周期 | 无 | 有 |
this | 无 | 有 |
state | 无 | 有 |
改变state | React Hooks:useState | this.setState() |
性能 | 高(不用实例化) | 低(需要实例化) |
其他区别
小例子说明问题:
- ProfilePageClass.js
import React from 'react';
class ProfilePageClass extends React.Component {
showMessage = () => {
alert('Folloed' + this.props.user);
};
handleclick = () => {
setTimeout(this.showMessage,3000);
};
render(){
return <button onClick={
this.handleclick}>Follow</button>
}
}
export default ProfilePageClass;
- ProfilePageFunction.js
import React from 'react';
function ProfilePageFunction(props){
const showMessage = () => {
alert('Followed'+ props.user);
}
const handleClick = () => {
setTimeout(showMessage,3000);