本文实例讲述了react 生命周期。分享给大家供大家参考,具体如下:
组件挂载:
componentWillMount(组件将要挂载到页面)->render(组件挂载中)->componentDidMount(组件挂载完成后)
组件更新:
1、shouldComponentUpdate(render之前执行,参数为ture时执行render,为false时不执行render)
componentWillUpdate(shouldComponentUpdate之后执行)
componentDidUpdate(render之后执行)
顺序:shouldComponentUpdate-》componentWillUpdate-》render-》componentDidUpdate
import React, { Component, Fragment } from 'react'; import List from './List.js'; class Test extends Component { constructor(props) { super(props); this.state={ inputValue:'aaa', list:['睡觉','打游戏'], } // this.add=this.add.bind(this); } addList() { this.setState({ list:[...this.state.list,this.state.inputValue], inputValue:'' }) } change(e) { this.setState({ // inputValue:e.target.value inputValue:this.input.value }) } delete(i) { // console.log(i); const list = this.state.list; list.splice(i,1); this.setState({ list:list }) } //组件将要挂载到页面时 componentWillMount() { console.log('componentWillMount'); } //组件完成挂载后 componentDidMount() { console.log('componentDidMount'); } //组件被修改之前,参数为ture时执行render,为false时不往后执行 shouldComponentUpdate() { console.log('1-shouldComponentUpdate'); return true; } //shouldComponentUpdate之后 componentWillUpdate() { console.log('2-componentWillUpdate'); } //render执行之后 componentDidUpdate() { console.log('4-componentDidUpdate'); } //组件挂载中 render() { console.log('3-render'); return ( <Fragment> <div> <input ref={(input)=>{this.input=input}} value={this.state.inputValue} onChange={this.change.bind(this)}/> <button onClick={this.addList.bind(this)}>添加</button> </div> <ul> { this.state.list.map((v,i)=>{ return( <List key={i} content={v} index={i} delete={this.delete.bind(this)} /> ); }) } </ul> </Fragment> ); } } export default Test;
2、componentWillReceiveProps(子组件中执行。组件第一次存在于虚拟dom中,函数不会被执行,如果已经存在于dom中,函数才会执行)
componentWillUnmount(子组件在被删除时执行)
import React, { Component } from 'react'; import PropTypes from 'prop-types'; class List extends Component { constructor(props) { super(props); this.delete = this.delete.bind(this); } //组件第一次存在于虚拟dom中,函数不会被执行 //如果已经存在于dom中,函数才会执行 componentWillReceiveProps() { console.log('componentWillReceiveProps'); } //子组件被删除时执行 componentWillUnmount() { console.log('componentWillUnmount'); } render() { return ( <li onClick={this.delete}>{this.props.name}{this.props.content}</li> ); } delete=() => { this.props.delete(this.props.index); } } List.propTypes={ name:PropTypes.string.isRequired, content:PropTypes.string, index:PropTypes.number, delete:PropTypes.func } //设置默认值: List.defaultProps={ name:'喜欢' } export default List;
组件性能优化:
import React, { Component } from 'react'; import PropTypes from 'prop-types'; class List extends Component { constructor(props) { super(props); this.delete = this.delete.bind(this); } //组件第一次存在于虚拟dom中,函数不会被执行 //如果已经存在于dom中,函数才会执行 componentWillReceiveProps() { console.log('componentWillReceiveProps'); } //子组件被删除时执行 componentWillUnmount() { console.log('componentWillUnmount'); } shouldComponentUpdate(nextProps,nextState) { if (nextProps.content !== this.props.content) { return true; } else { return false; } } render() { return ( <li onClick={this.delete}>{this.props.name}{this.props.content}</li> ); } delete=() => { this.props.delete(this.props.index); } } List.propTypes={ name:PropTypes.string.isRequired, content:PropTypes.string, index:PropTypes.number, delete:PropTypes.func } //设置默认值: List.defaultProps={ name:'喜欢' } export default List;
希望本文所述对大家react程序设计有所帮助。
标签:
react,生命周期
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“react 生命周期实例分析”评论...
更新动态
2024年05月03日
2024年05月03日
- 张玮伽《一生所爱》[DSF][1.5G]
- 张信哲《思念(粤语) 》[WAV分轨][433M]
- 原声《重庆森林电影原声带》[FLAC][498M]
- 群星《宝丽金50半世纪之歌》[FLAC][802M]
- 姜育恒.1989-精品集爱我·倾诉【瑞华】【WAV+CUE】
- 水木年华.2004-毕业纪念册【水木同创】【WAV+CUE】
- [7504]ReferenceSoundEdition-GreatGuitarTunes(2013)[GermanyHQCD]
- 刘嘉佳《往日时光HQ》头版限量[低速原抓WAV+CUE]
- 单依纯《纯情歌》开盘母带[低速原抓WAV+CUE]
- 杨乐婷《遥望HQ》头版限量编号[低速原抓WAV+CUE]
- 蔡琴《三十周年纪念版》6N纯银镀膜[正版CD低速原抓WAV+CUE][519M]
- 张学友《昨夜梦魂中》[FLAC][453M]
- 群星《17号音乐仓库2 第9期》[320K/MP3][150.23MB]
- 汤兰花.2000-一代佳人(环球唱片金曲系列风华再现)【WAV+CUE】
- 罗美玲.2005-生日领悟(EP)【华研国际】【WAV+CUE】