每天一点点之 taro 框架 - 生命周期 & state
注意:从vue过来的小朋友要注意,taro直接赋值时不会更新组件的,同react一致更新数据必须调用setState方法,例如:this.setState({name:'张三'})
1.render函数
return中的标签可以是但标签,也可以是多标签
2.index.js文件内容介绍
import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import './index.scss'
export default class Index extends Component {
config = {
navigationBarTitleText: '首页'
}
componentWillMount () {
console.log('componentWillMount,第一次渲染之前执行,只执行一次')
}
componentDidMount () {
console.log('componentDidMount,第一次渲染之后执行,只执行一次')
}
componentWillUnmount () {
console.log('componentWillUnmount,卸载时执行')
}
componentWillUpdate () {
console.log('componentWillUpdate,state数据将要更新')
}
componentDidUpdate () {
console.log('componentDidUpdate,state数据更新过后')
}
shouldComponentUpdate (nextProps,nextState) {
// 检查此次setState是否要进行render调用,返回true调用,false不调用
// 一般用来多次的setState调用时,提升render性能
// 判断状态
if(nextState.state.text == '李四')
return true;
}
componentWillReceiveProps(nextProps){
// 会在父组件传递给子组件的参数发生改变时触发
}
// componentDidShow与componentDidHide在reactjs不存在,是taro为了兼容小程序实现的
componentDidShow () {
console.log('componentDidShow,显示时触发')
}
componentDidHide () {
console.log('componentDidHide,隐藏时触发')
}
state = {
name:'Helloworld'
}
getName () {
return 111;
}
render () {
console.log('render,第一次运行')
return (
<View className='index'>
<View>获取动态变量:{this.state.name}</View>
<View>获取动态方法:{this.getName()}</View>
</View>
)
}
}
3.tarojs的生命周期
tarojs有六个生命周期

4.setState更新异步问题
setState方法有两个参数, 第一个时对象,第二个时回调函数
componentDidMount () {
console.log('componentDidMount,第一次渲染之后执行,只执行一次')
this.setState({name:'王五'},()=>{
console.log(this.state.name,'回调')
})
console.log(this.state.name,'同步')
}
由运行结果可以看出,setState方法是异步的

每天一点点之 taro 框架 - 生命周期 & state的更多相关文章
- 每天一点点之 taro 框架开发 - taro路由及传参
1.路由 taro的路由是自带的,不需要我们额外配置,只需要我们在app.js下config中配置pages即可 class App extends Component { config = { pa ...
- 3) drf 框架生命周期 请求模块 渲染模块 解析模块 自定义异常模块 响应模块(以及二次封装)
一.DRF框架 1.安装 pip3 install djangorestframework 2.drf框架规矩的封装风格 按功能封装,drf下按不同功能不同文件,使用不同功能导入不同文件 from r ...
- Libgdx 开发指南(1.1) 应用框架——生命周期
生命周期 Libgdx应用有一个定义好的生命周期,控制着整个应用的状态,例如creation, pausing, resuming, disposing ApplicationListener 开发者 ...
- 每天一点点之 taro 框架开发 - 事件处理与样式表
1.方法调用 state = { name:'张三' } test(){ this.state.name } <button onClick={ this.test.bind(this) } / ...
- 每天一点点之 taro 框架开发 - taro调用组件传值
1.调用组件 组件文件 import Taro, { Component } from '@tarojs/taro' import { View } from '@tarojs/components' ...
- 每天一点点之 taro 框架开发 - taro静态资源引入
1.说明: taro中客园自由的引用静态资源,不需要安装任何的loader 引用样式文件 通过ES6的import引入 2.引用js文件 import { functionName } from '. ...
- Laravel 入口文件解读及生命周期
这里只贴index.php的代码, 深入了解的请访问 https://laravel-china.org/articles/10421/depth-mining-of-laravel-life- ...
- React State&生命周期
State&生命周期 State&生命周期 到目前为止我们只学习了一种方法来更新UI. 我们调用ReactDOM.render()来改变输出: function tick(){ con ...
- 【技术博客】Flutter—使用网络请求的页面搭建流程、State生命周期、一些组件的应用
Flutter-使用网络请求的页面搭建流程.State生命周期.一些组件的应用 使用网络请求的页面搭建流程 在开发APP时,我们常常会遇到如下场景:进入一个页面后,要先进行网络调用,然后使用调用返 ...
随机推荐
- SQL 笔记1,left join,group by,having
表:XS,XK,CJ left join 表1 on 表1.字段=表2.字段 group by 分组条件 order by 排序条件 asc正序(小到大),desc倒序 having 跟条件类似whe ...
- 多门店4s管理系统
下载 系统登录用户名与密码:manage/123456
- 【CF1217F】Forced Online Queries Problem
题意 题目链接 动态图连通性,加密方式为 \((x+l-1)\bmod n +1\) (\(l=[上一次询问的两点连通]\)). 点数 \(n\),操作数 \(m\) \(\le 2\times 10 ...
- 2018--Linux面试题
1.企业场景面试题:buffer与Cache的区别. 2.企业场景面试题:redhat与CentOS的区别. 3.企业场景面试题: 描述RAID 0 1 5 10的特点. 4.企业场景面试题:32位 ...
- windows目标进程注入dll
在别的程序注入dll 步骤: ,获取目标进程ID,CreateToolhelp32Snapshot()函数; ,获取目标进程句柄,OpenProcess()函数; ,目标进程要一块内存,Virtual ...
- PAT (Advanced Level) 1140~1143:1140模拟 1141模拟 1142暴力 1143 BST+LCA
1140 Look-and-say Sequence(20 分) 题意:观察序列D, D1, D111, D113, D11231, D112213111, ...,显然后一个串是对前一个串每一小段连 ...
- word2010文档如何隐藏右侧灰色空白不可编辑区域
word2010文档如何隐藏右侧灰色空白不可编辑区域, (word2007也是差不多的操作) 两种方法: 1.点击图中的“最终状态”按钮: 2.点击图中的”以嵌入方式显示所有修订“的按钮:
- vCenter 导入Windows Server 2003/XP自定义规范失败
1.vcsa 切换到/etc/vmware-vpx/sysprep目录下,会有很多个目录,根据Windows Server 2003的版本,64位找到 svr2003-64 这个目录,32位找到svr ...
- 从植发AI看智能手术机器人的国产化之路
在工作和生活的双重压力下,很多80后乃至90后的青年都"光荣"地加入了"脱发一族".为了拯救发际线或是不变成"地中海",很多人从此走上了寻医 ...
- 关于netty配置的理解serverBootstrap.option和serverBootstrap.childOption
The parameters that we set using ServerBootStrap.option apply to the ChannelConfig of a newly create ...