系列文章传送门:
react 进阶必学 hook (一):useState 来一碗大碗宽面
react 进阶必学 hook (二):useEffect 专治不吃宽面
react 进阶必学 hook (三):useContext 面馆分店开张了
在理解useEffect
之前我们先看一个例子,还是面馆:
面馆例子
承接上回react.js: 进阶必学 hook (一):useState 来一碗大碗宽面
我们在之前的基础上添加一个需求,就是这个面馆只卖宽面,不点宽面不欢迎👎
先用class组件的形式实现一下:
export default class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 1,
big: true,
type:'宽面'
};
}
componentDidMount() {
if(this.state.type !== '宽面'){
window.alert('啥?你不吃宽面!!?小吴,把我的意大利炮拿来!😡')
}
}
componentDidUpdate() {
if(this.state.type !== '宽面'){
window.alert('啥?你不吃宽面!!?小吴,把我的意大利炮拿来!😡')
}
}
render () {
const {
count,big,type} = this.state;
const size = (big)=>big? "大":"小";
return (
<div>