学习过react的同学一定知道 state
状态State
状态更新一定是异步的
设置状态必须用setState
-- 同步更新需要传入callBack
import Taro, { Component } from "@tarojs/taro";
import { View, Text, Button } from "@tarojs/components";
import "./index.less";
export default class Index extends Component {
componentWillMount() {}
componentDidMount() {}
componentWillUnmount() {}
componentDidShow() {}
componentDidHide() {}
config = {
navigationBarTitleText: "首页"
};
state = {
name: "张思学"
};
setName() {
this.setState({ name: '张思学6666' },()=>{
console.log('callBack', this.state.name);
});
console.log('旧的', this.state.name);
}
render() {
return (
<View className="index">
<Text>{this.state.name}</Text>
<Button onClick={this.setName}>click</Button>
</View>
);
}
}
网友评论