children属性传值与生命周期函数

children属性传值
在Hello.js
import React, { Component } from 'react'

export class Hello extends Component {
  render() {
    return (
      <div>
        <h1>Hello:{this.props.name}</h1>
        <hr></hr>
        <h2>{this.props.children}</h2>
      </div>
    )
  }
}

export default Hello

在Wolild .js
import React, { Component } from 'react'

export class Wolild extends Component {
  render() {
    return (
      <div>WXQ</div>
    )
  }
}

export default Wolild

在index.js
import ReactDOM from 'react-dom';
import React from 'react';
import { Hello as Hello1 } from './children属性传值/Hello';
import { Wolild } from './children属性传值/Wolild';
ReactDOM.render(<Hello1 name={'ZY'}><Wolild /></Hello1>, document.getElementById('root'));

生命周期函数
分为三个阶段(创建阶段 , 更新阶段,销毁阶段)
创建阶段
import React, { Component } from 'react'

export class App extends Component {
  constructor() {
    super()
    this.state = {
      count: 0
    }
    console.log('1 constructor');
  }
  render() {
    console.log('2 render');
    // 不能使用setState()
    return <div>{this.state.count}</div>
  }
  componentDidMount() {
    console.log('3 componentDidMount');
    // 发网络请求
    // 拿到真是DOM
  }

}

export default App

更新阶段
import React, { Component } from 'react'

export class App extends Component {
  constructor() {
    super()
    this.state = {
      count: 0
    }
    console.log('1 constructor');
  }
  render() {
    console.log('2 render');
    // 不能使用setState()
    return (
      <>
        <div id='1'>{this.state.count}</div>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>+1</button>
      </>
    )
  }
  componentDidMount() {
    console.log('3 componentDidMount');
    // 发网络请求
    // 拿到真是DOM
  }
  componentDidUpdate() {
    // 不能直接调用setState会导致无限循环
    // 可以有条件的使用setState
    console.log('4 componentDidUpdate');
    // 也可以拿到DOM(是最新的)
    console.log('id', document.getElementById('1'));
  }

}

export default App

销毁阶段
import React, { Component } from 'react'

export class App extends Component {
  constructor() {
    super()
    this.state = {
      count: 0
    }
    console.log('1 constructor');
  }
  render() {
    console.log('2 render');
    // 不能使用setState()
    return (
      <>
        <div id='1'>{this.state.count}</div>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>+1</button>
        {this.state.count > 3 ? null : <Child />}
      </>
    )
  }
  componentDidMount() {
    console.log('3 componentDidMount');
    // 发网络请求
    // 拿到真是DOM
  }
  componentDidUpdate() {
    // 不能直接调用setState会导致无限循环
    // 可以有条件的使用setState
    console.log('4 componentDidUpdate');
    // 也可以拿到DOM(是最新的)
    console.log('id', document.getElementById('1'));
  }

}
export class Child extends Component {
  constructor() {
    super()
    this.state = {
      count: 0
    }
    console.log('子=>1 constructor');
    window.addEventListener('resize', this.handleRrsize)
  }
  handleRrsize = () => {
    console.log('resize....');
  }
  render() {
    console.log('子=>2 render');
    // 不能使用setState()
    return (
      <>
        <div id='1'>{this.state.count}</div>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>+1</button>
      </>
    )
  }
  componentDidMount() {
    console.log('子=>3 componentDidMount');
    // 发网络请求
    // 拿到真是DOM
  }
  componentDidUpdate() {
    // 不能直接调用setState会导致无限循环
    // 可以有条件的使用setState
    console.log('子=>4 componentDidUpdate');
    // 也可以拿到DOM(是最新的)
    console.log('id', document.getElementById('1'));
  }
  componentWillUnmount() {
    console.log('子=>5 componentWillUnmount');
    window.removeEventListener('resize', this.handleRrsize)
  }

}

export default App

setstate传递回调函数
import React, { Component } from 'react'

export class SetStatejs extends Component {
  state = {
    count: 1
  }
  handleCount = () => {
    this.setState({
      count: 2
    })
    this.setState(state => {
      console.log(state.count);
      return {
        count: state.count + 1
      }
    })
  }

  render() {
    return (
      <div>
        <h1>{this.state.count}</h1>
        <button onClick={this.handleCount}>+1</button>
      </div>
    )
  }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值