React(6)

1.React插槽

import React, { Component } from 'react'
import Child from './compoent/Child'

export default class App extends Component {
    render() {
        return (
            <div>
                <Child>
                    <div>App下的div</div>
                </Child>
            </div>
        )
    }
}

import React, { Component } from 'react'

export default class Child extends Component {
    render() {
        return (
            <div>
                Child
            </div>
        )
    }
}

可以发现在Child组件内部的 <div>App下的div</div>并没有被渲染出来!!!!

应该使用react插槽:{this.props.children}   固定写法

多个的话,也可以指定第几个

 2.React生命周期

2.1初始化阶段 (执行一次)

componentWillMount()  :render之前最后一次修改状态的机会

componentDidMount() :成功render并渲染完成真实DOM之后触发,可以修改DOM

render()

执行顺序:componentWillMount--》render--》componentDidMount

import React, { Component } from 'react'

export default class App extends Component {


    state={
        myname:"www"
    }

    componentWillMount(){
        console.log("componentWillMount()");
        this.setState({
            myname:"xxx"
        })

    }


    componentDidMount(){
        console.log("componentDidMount()");
        //数据请求axios
        //订阅函数调用
        //setInterVal
        //基于创建完的dom进行 ....
    //....

    }


    render() {
        console.log("render()");
        return (
            <div>
                App---{this.state.myname}
            </div>
        )
    }
}

 

警告:

componentWillMount官方已经不推荐使用了。componentWillMount已被重命名,不建议使用。

可以将代码移动到componentDidMount,或者在构造函数中设置初始状态。

可以将componentWillMount重命名为UNSAFE_componentWillMount,以在非严格模式下抑制此警告。

初始化案例:

import React, { Component } from 'react'
import BetteScroll from 'better-scroll'


export default class App extends Component {


    state = {
        list: ["11111", "22222", "33333", "44444", "55555", "66666", "77777"]
    }

    componentDidMount() {
        //    在dom渲染完成后使用
        new BetteScroll("#listdiv"); 

    }


    render() {

        return (
            <div id="listdiv" style={{background:'yellow',overflow:'hidden',height:'100px',width:'300px'}}>
                <ul>
                    {this.state.list.map((item) =>
                        <li key={item}>{item}</li>)}
                </ul>

            </div>
        )
    }
}

2.2 运行中阶段 (执行多次)

componentWillUpdate()  :执行更新前调用

componentDidUpdate(prevProps,prevState)  :执行更新完毕后调用。可以有两个形参 ,分别代表更新前旧的属性,旧的状态。

shouldCompoentUpdate(nextProps,nextState) :执行更新前调用,询问是否需要更新,返回false会阻止render调用。     性能优化函数

render()

componentWillReceiveProps(nextProps) :父组件修改属性时触发。最先获得父组件传来的属性,并可以利用属性进行ajax或者逻辑处理

2.3 销毁阶段

componentWillUnmount  :在删除组件之前进行清理操作,比如计时器和时间监听器

{this.state.next==='aaaa' && <Field/>}  这种形式的创建子组件就会存在删除组件

3.老生命周期的问题

(1) componentWillMount ,在ssr中 这个方法将会被多次调用, 所以会重复触发多遍,同时在这里如果绑定事件, 将无法解绑,导致内存泄漏 , 变得不够安全高效逐步废弃。

(2) componentWillReceiveProps 外部组件多次频繁更新传入多次不同的 props,会导致不必要的异步请求

(3) componetWillupdate, 更新前记录 DOM 状态, 可能会做一些处理,与componentDidUpdate相隔时间如果过 长, 会导致 状态不太信

4.新生命周期的替代

(1)getDerivedStateFromProps 第一次的初始化组件以及后续的更新过程中(包括自身状态更新以及父传子) , 返回一个对象作为新的state,返回null则说明不需要在这里更新state。

//老的生命周期的写法
componentDidMount() {
    if (this.props.value !== undefined) {
        this.setState({
            current: this.props.value
        })
    }
}
componentWillReceiveProps(nextProps){
    if (nextProps.value !== undefined) {
        this.setState({
            current: nextProps.value
        })
    }
}


// 新的生命周期写法
static getDerivedStateFromProps(nextProps,nextState) {
    if (nextProps.value !== undefined) {
        return {
            current: nextProps.value
        }
    }
    return null
}

4.react中性能优化的方案

1. shouldComponentUpdate

        控制组件自身或者子组件是否需要更新,尤其在子组件非常多的情况下, 需要进行优化。

2. PureComponent

        这里改为PureComponent

       

         PureComponent会帮你 比较新props 跟 旧的props, 新的state和老的state(值相等,或者 对象含有相同的属性、且属性值相等 ),决定shouldcomponentUpdate 返回true 或者 false, 从而决定要不要呼叫 render function。

注意: 如果你的 state 或 props 『永远都会变』,那 PureComponent 并不会比较快,因为 shallowEqual 也需要花时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java-请多指教

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值