剧场模式
首页前端React诱人的 react 视频教程-基础篇

诱人的 react 视频教程 - 基础篇 #14 组件生命周期(完结)

海外散仙厉飞雨 · 真仙发布于
7

学习完此套教程的同学,可以移步实践篇:react 基础实践篇-小型财务系统

http://www.runoob.com/react/react-component-life-cycle.html

https://reactjs.org/docs/react-component.html

src/App.js

import React, { Component } from 'react';

import Header from './components/Header';
import Home from './components/Home';

class App extends Component {
  constructor() {
    super();
    this.state = {
      homeLink: "Home",
      homeMounted: true
    }
  }

  onGreet(age) {
    alert(age);
  }

  onChangeLinkName(newName) {
    this.setState({
      homeLink: newName
    })
  }

  onChangeHomeMounted() {
    this.setState({
      homeMounted: !this.state.homeMounted
    })
  }

  render() {
    const user = {
      name: "Anna",
      hobbies: ["Sports", "Reading"]
    }
    let homeCmp = "";
      if (this.state.homeMounted) {
        homeCmp = (
          <Home
            name={"Max"}
            initialAge={12}
            user={user}
            greet={this.onGreet}
            changeLink={this.onChangeLinkName.bind(this)}
            initialName={this.state.homeLink}
          />
        );
      }
    return (
      <div className="container">
        <div className="row">
          <div className="col-xs-1 col-xs-offset-11">
            <Header homeLink={this.state.homeLink} />
          </div>
        </div>
        <div className="row">
          <div className="col-xs-1 col-xs-offset-11">
            <h1>Hello !!</h1>
          </div>
        </div>
        <div className="row">
          <div className="col-xs-1 col-xs-offset-11">
            {homeCmp}
          </div>
        </div>
        <hr />
        <div className="row">
          <div className="col-xs-1 col-xs-offset-11">
            <button onClick={this.onChangeHomeMounted.bind(this)} className="btn btn-primary">(Un)mount Home Component</button>
          </div>
        </div>
      </div>
    );
  }
}

export default App;

src/components/Home.js

import React, { Component } from 'react';

import PropTypes from 'prop-types';

export default class Home extends Component {
  constructor(props) {
    super(props);
    this.state = {
      age: props.initialAge,
      status: 0,
      homeLink: props.initialName
    }
    setTimeout(() => {
      this.setState({
        status: 1
      })
    }, 3000)
    console.log('Constructor');
  }

  onMakeOlder() {
    this.setState({
      age: this.state.age + 3
    })
  }

  handleGreet() {
    this.props.greet(this.state.age)
  }

  onChangeLink() {
    this.props.changeLink(this.state.homeLink);
  }

  onHandleChange(event) {
    this.setState({
      homeLink: event.target.value
    })
  }

  componentWillMount() {
    console.log("Component will mount");
  }

  componentDidMount() {
    console.log("Component did mount");
  }

  componentWillReceiveProps(nextProps) {
    console.log('Component will receive props', nextProps);
  }

  shouldComponentUpdate(nextProps, nextState) {
    console.log('Component should update', nextProps, nextState);
    if (nextState.status === 1) {
      return false;
    }
    return true;
  }

  componentWillUpdate(nextProps, nextState) {
    console.log('Component will update', nextProps, nextState);
  }

  componentDidUpdate(prevProps, prevState) {
    console.log('Component did update', prevProps, prevState);
  }

  componentWillUnmount() {
    console.log('Component will unmount');
  }

  render() {
    console.log('render');
    return (
      <div className="container">
        <div className="row">
          <div className="col-xs-1 col-xs-offset-11">
            <div>your name is {this.props.name}, your age is {this.state.age}</div>
            <p>Status: {this.state.status}</p>
            <button onClick={() => {this.onMakeOlder()}} className="btn btn-primary">Make me older</button>
            <hr />
            <button onClick={this.handleGreet.bind(this)} className="btn btn-primary">Greet</button>
            <hr />
            <input
              type="text"
              defaultValue={this.props.initialName}
              value={this.state.initialName}
              onChange={(event) => this.onHandleChange(event)}
            />
            <button onClick={this.onChangeLink.bind(this)} className="btn btn-primary">Change Header Link</button>
          </div>
        </div>
      </div>
    );
  }
}

Home.propTypes = {
  name: PropTypes.string,
  age: PropTypes.number,
  user: PropTypes.object,
  greet: PropTypes.func,
  initialName: PropTypes.string
};
5 条回复
学员
gitwisdom · 真仙ttyring · 真仙me · 真仙caspertu · 金仙后期chenjimin132 · 金仙初期suntong233 · 真仙开心就好 · 金仙后期linmiaomiao123 · 真仙CJY · 真仙Qujoe · 真仙libin · 金仙巅峰FastTurbo · 大罗super · 金仙巅峰ttswh2009 · 太乙巅峰woshimaxiaorui · 太乙玉仙GmUy9xfK · 真仙wsadczh · 真仙bigHippoZz · 真仙YJwsl · 金仙中期itlaowang520 · 太乙巅峰duzhihao · 金仙后期没有昵称 · 真仙pdfun · 真仙Vintage · 真仙jixiyu · 真仙ldn · 真仙远方。 · 真仙00Jane · 金仙后期shaoqiming1028 · 真仙子非鱼 · 太乙玉仙shangxuewei · 真仙zwkjhx · 真仙Kelly · 真仙huxiaohui · 真仙a653398363 · 真仙qq540618173 · 真仙liunian1993 · 金仙后期hyt9796 · 金仙巅峰lbb · 金仙后期LINHANXIN · 真仙shmily1996 · 金仙后期h23 · 大罗linhaijian · 真仙Godvmiku · 真仙Messi-Q · 真仙ralis366 · 真仙samuelshang · 大罗007sair · 真仙QQ741404277 · 金仙中期dannyking630 · 太乙玉仙
最新动态
gitwisdom · 真仙学习到了21:09
ttyring · 真仙学习到了0:00
me · 真仙学习到了2:37
caspertu · 金仙后期学习到了0:15
chenjimin132 · 金仙初期学习到了5:46

© 汕尾市求知科技有限公司 | 粤ICP备19038915号 | 在线学员:32

Top