学习react第六天

学习react第六天

react的生命周期函数

  1. 更新的时候 constructor>render>componentMount;
  • 2.修改的时候 render>componentUpdate
    • 1.更新setState的时候render重新渲染会调用render和componentDidUpdate这两个方法
    • 2.Props如果子组件接收了父组件的数据而父组件使用(setState)的数据改变了那么子组件的render也会触发
    • 3.forceUpdate 调用这个方法不管页面有没有更新都会导致重新渲染类似于页面刷新(强制页面更新)
  1. 销毁的时候 component
// 父组件
class LifeFun  extends react.Component{
  // 初始化state和绑定的this
  constructor(){
    super()
    this.state={
      name:"shen",
    }
  }
  update=()=>{
  this.setState({
    name:"li"
  })
  this.forceUpdate();
  }
  // 页面渲染的时候
 render(){
  //  注意不能在render调用setState发给发
   console.log("这是生命周期函数2")
   return (<span>{this.state.name}<button onClick={this.update}>点击改变数据</button> <Child name={this.state.name} ></Child></span>)
 }
//  1.发送网络请求
//  2. DOM操作
// 页面挂载的时候
 componentDidMount(){
   console.log("3")
 }
//  数据更新的时候
 componentDidUpdate(){
   console.log("页面更新的时候触发")
 }
 
}

// 子组件
class Child extends react.Component{
  render(){
    console.log("@")
    return (<span>{this.props.name}子组件</span>)
  }
  componentDidUpdate(){
    console.log("子组件页面重新渲染了")
  }
  componentWillUnmount(){
    console.log("页面销毁的时候")
  }
}

LifeFun.propTypes={
  name:propsTypes.string
}

reactDOM.render(<LifeFun></LifeFun>,document.querySelector("#root"))

组件的复用 props的render和childrne方法

使用render复用组件

//  1.创建复用组件,提供逻辑状态代码
//  2.将状态(数据)使用render 暴露在组件外部  render(){return this.props.render(state)}
//  3.render(){return data}render可以将数据暴露在外部使用 
// 这个是render方法
export default class Mouse extends react.Component{
  state={
    x:0,
    y:0,
  }
  mover=(e)=>{
    this.setState({
      x:e.pageX,
      y:e.pageY
    })
  }
  componentDidMount(){
    window.addEventListener("mousemove",(e)=>{
      this.mover(e)
    })
  }
  render(){
    return this.props.render(this.state)
  }
}
  reactDOM.render(<Mouse render={(state)=>{return (<div><p>Y:{state.x},X:{state.y}</p><img src='https://img2.baidu.com/it/u=2108319215,1494231136&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=690' style={{width:50+"px",position:'absolute',left:`${state.x-25}px`,top:`${state.y-34.5}px`}}></img></div>)}}></Mouse>,document.querySelector("#root")) 

使用children方法复用组件

//  1.创建复用组件,提供逻辑状态代码
//  2.将状态(数据)使用render 暴露在组件外部  render(){return this.props.render(state)}
//  3.render(){return data}render可以将数据暴露在外部使用 
// 推荐把render替换成children方式
// 把render替换成children
export default class  ReplaceMouse extends react.Component{
  state={
    x:0,
    y:0,
  }
  mover=(e)=>{
    this.setState({
      x:e.pageX,
      y:e.pageY
    })
  }
  componentDidMount(){
    window.addEventListener("mousemove",(e)=>{
      this.mover(e)
    })
  }
  render(){
    // 如果使用childrne方法就把 this.props.render(this.state)替换成 this.props.children(this.state)
    return this.props.children(this.state)
  }
}
reactDOM.render(<ReplaceMouse >{(data)=>{return <p>
Y:{data.y}-X:{data.x}
</p>}}</ReplaceMouse>,document.querySelector("#root")) 

高阶组件的初次使用

// 高阶组件hoc
// 高阶组件:是一个函数,接收包装的组件,返回增强后的组件

创建步骤

  1. 创建一个函数,名称定位wite(也可以是其他名字)
  2. 指定函数的参数,参数大写字母(作为要渲染的组件)
  3. 在函数内部创建一个类组件,提供复用(状态(数据)的逻辑代码),并返回
  4. 在render返回函数的参数(组件) render(){returb }可以传入props数据
  5. 传入数据后 在 渲染组件里面通过props调用数据
  6. 通过函数名字 mouse(App) 增强组件 通过返回值拿到组件 let Mouse=mouse(App) 在通过Mouse渲染到页面上
// 创建高阶组件HOC 复用组件
function mouse(Component){
  class Mouse extends react.Component{
    constructor(){
      super();
      this.name="shen"
    }
    state={
      x:null,
      y:null,
    }
    hand(e){
      this.setState({
        x:e.pageX,
        y:e.pageY,
      })
    }
    componentDidMount(){
      window.addEventListener("mousemove",(e)=>{
        this.hand(e)
      })
    }
    render(){
      // 这里返回id时候函数里面的参数是一个组件
      return <Component {...this.state} name={this.name}></Component>
    }
  }
  return Mouse
}


// 页面的渲染
const App=(props)=>{
  return (<span>{props.x}-{props.y}-{props.name}</span>)
}

let Mouse=mouse(App)
reactDOM.render(<Mouse></Mouse>,document.querySelector("#root"))
React学习手册第二版》是一本关于React框架的学习指南,它提供了对React的全面介绍和深入的学习资料。该手册由React社区的专家编写,内容准确、权威。 该手册分为它共分为六个主要部分。第一部分是React入门指南,介绍了React的基本概念、核心特性和使用方法,帮助读者快速上手。第二部分是组件开发,详细介绍了React中的组件的创建、生命周期、数据传递等方面的知识。第三部分是状态管理,探讨了如何使用Redux等库来管理React应用的状态,提高应用的可维护性和可扩展性。第四部分是路由和导航,讲解了如何使用React Router等库来管理应用的导航结构和URL的变化。第五部分是React生态系统,介绍了React常用的相关工具和库,如Webpack、Babel等,帮助读者提升开发效率。最后一部分是React应用的部署,讲解了如何将React应用发布到生产环境中,并提供了一些优化和调试技巧。 《React学习手册第二版》的特点是内容全面、结构清晰,适合初学者和有一定基础的开发者阅读。它采用了实例驱动的教学方法,通过大量的案例和实践来帮助读者理解和掌握React的相关知识。此外,这本手册还提供了丰富的参考资料和扩展阅读,方便读者深入学习和研究。 总之,《React学习手册第二版》是一本很好的学习资料,无论是初学者还是有一定经验的开发者,都可以从中获取到丰富的知识和实践经验,帮助他们深入了解和应用React框架。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值