微信小程序的專屬方法如下
onPullDownRefresh //頁(yè)面相關(guān)事件處理函數(shù)–-監(jiān)聽(tīng)用戶下拉動(dòng)作,從而刷新頁(yè)面
onReachBottom //頁(yè)面上拉觸底事件的處理函數(shù)--用于分頁(yè)處理刷新頁(yè)面
onShareAppMessage //用戶點(diǎn)擊右上角轉(zhuǎn)發(fā)
onPageScroll //頁(yè)面滾動(dòng)觸發(fā)事件的處理函數(shù)
onTabItemTap // 當(dāng)前是 tab 頁(yè)時(shí),點(diǎn)擊 tab 時(shí)觸發(fā)。這個(gè)事件本身還有缺陷待修復(fù)。
componentWillPreload //預(yù)加載,只在微信小程序中可用
一般組件完整的生命周期如下
componentWillMount //在微信小程序中這一生命周期方法對(duì)應(yīng)頁(yè)面的onLoad或入口文件app.js中的onLaunch
componentDidMount //在微信小程序中這一生命周期方法對(duì)應(yīng)頁(yè)面的onReady或入口文件app.js中的onLaunch,在 componentWillMount后執(zhí)行
componentDidShow //在微信小程序中這一生命周期方法對(duì)應(yīng) onShow
componentDidHide //在微信小程序中這一生命周期方法對(duì)應(yīng) onHide
componentDidCatchError //錯(cuò)誤監(jiān)聽(tīng)函數(shù),在微信小程序中這一生命周期方法對(duì)應(yīng) onError
componentDidNotFound //頁(yè)面不存在監(jiān)聽(tīng)函數(shù),在微信小程序中這一生命周期方法對(duì)應(yīng) onPageNotFound
shouldComponentUpdate //頁(yè)面是否需要更新
componentWillUpdate //頁(yè)面即將更新
componentDidUpdate //頁(yè)面更新完畢
componentWillUnmount //頁(yè)面退出,在微信小程序中這一生命周期方法對(duì)應(yīng) onUnload
入口文件繼承自 Component 組件基類,它的生命周期不如組件完整,包含如下:
componentWillMount、componentDidMount、componentDidShow、componentDidHide、componentDidCatchError、componentDidNotFound
修改state的任意數(shù)據(jù)
// https://www.cnblogs.com/zjlx/
this.state = {
current: 0,
tabObj: {title: '未使用',text:'已使用'},
dataList: [
{name: 'jack', age: 28},
{name: 'rose', age: 18}
]
}
// =========
this.setState((preState) => {
//修改基本類型數(shù)據(jù)
preState.current = e.detail.value;
//修改某一對(duì)象屬性
preState.tabObj.title = e.detail.value;
//修改整個(gè)對(duì)象
preState.tabObj = {title:e.detail.value,text:'xxx'}
//修改某一數(shù)組對(duì)象屬性
preState.dataList[0].name = e.detail.value;
//修改某一數(shù)組對(duì)象全部屬性
preState.dataList[0] = {
name: e.detail.value,
age: 9
};
//修改整個(gè)數(shù)組
preState.dataList = [
{
name: e.detail.value,
age: 99
},
{
name: e.detail.value,
age: 88
},
];
}, () => {
console.log(this.state.current);
console.log(this.state.dataList);
console.log(this.state.dataList);
})
propTypes = {
optionalArray: PropTypes.array,//檢測(cè)數(shù)組類型
optionalBool: PropTypes.bool,//檢測(cè)布爾類型
optionalFunc: PropTypes.func,//檢測(cè)函數(shù)(Function類型)
optionalNumber: PropTypes.number,//檢測(cè)數(shù)字
optionalObject: PropTypes.object,//檢測(cè)對(duì)象
optionalString: PropTypes.string,//檢測(cè)字符串
optionalSymbol: PropTypes.symbol,//ES6新增的symbol類型
}