React生命周期
发布时间:2020-12-15 20:26:21 所属栏目:百科 来源:网络整理
导读:import React,{Component,PureComponent} from ‘react‘import ReactDOM from ‘react-dom‘import ‘bootstrap/dist/css/bootstrap.css‘/* * 如果props或者是state里面给的内容一样,Component也会走一遍历更新确认 *//* React.createElement() React.cre
import React,{Component,PureComponent} from ‘react‘import ReactDOM from ‘react-dom‘import ‘bootstrap/dist/css/bootstrap.css‘/* * 如果props或者是state里面给的内容一样,Component也会走一遍历更新确认 *//* React.createElement() React.createContext() React.createFactory() React.createRef() React.cloneElement()*/class Vote extends Component { constructor(props,context,updater) { super(props); console.log("构造方法1") this.state = { times: 0 } } render() { console.log(‘render方法3‘); let {times} = this.state return ( <div> {times} <button className=‘btn btn-danger‘ onClick={(e) => { this.handClick("更改",e) }}>更改 </button> </div> ) } handClick(flag,e) { //也可以这样设置e // this.setState((prevState,props) => { // return {times: prevState.times}; // }); //两个版本的内容,加大括号{}的则要,则要加return 可以换成()里面则不要加Return this.setState((prevState,props) => ({times: prevState.times + 1})); } componentWillMount() { console.log("组件将要加载2"); } componentDidMount() { console.log("组件已经加载4"); }// 在组件更新前进行判断 ,要不要更新 shouldComponentUpdate(nextProps,nextState) { console.log("是否要更新"); return true; }//在同意之后 ,则组件做一个通知,组件即将更新 componentWillUpdate(nextProps,nextState) { console.log("组件将要更新",nextState.times); }//在组件更新完成后,提示完成更新 componentDidUpdate(prevProps,prevState,snapshot) { console.log("组件已经更新",prevState.times); console.log(snapshot); } componentWillReceiveProps (nextProps,nextContext){ console.log(nextProps); }}ReactDOM.render(<div>aaaaaaaaaa <Vote/></div>,root); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |