React性能优化:子组件多次渲染解决(meno)

针对函数组件

背景:
当子组件的接收的props,前后对比没有改变时,不渲染组件。

在这里插入图片描述

在这里插入图片描述

Read/index.jsx父组件

import React, { useState } from 'react';
import List from './components/List';
import Card from './components/Card';
import './index.scss';

export default function Read(pros) {
    console.log('redner-read');

    const [list1, setList1] = useState(0);
    const [card1, setCard1] = useState(0);
    const [card2, setCard2] = useState(0);

    const cardGetCount1 = (value) => {
        setCard1(value)
    }
    const cardGetCount2 = (value) => {
        setCard2(value)
    }
    const listGetCount1 = (value) => {
        setList1(value)
    }
    return (
        <>
            <div className='top_READ'>
                <h2> Read</h2>
                <List
                    value1={list1}
                    listGetCount1={listGetCount1}
                />
                <br />
                <Card
                    value1={card1}
                    value2={card2}
                    cardGetCount1={cardGetCount1}
                    cardGetCount2={cardGetCount2}
                />
            </div>
        </>
    )
}

List/index.jsx子组件

import React, { useState, memo } from 'react';
import Button from '../../../../components/Button';
const areEqual = (prevProps, nextProps) => {
    if (prevProps?.value1 === nextProps?.value1) {
            return true
    }
    return false
}
function List(props) {
    const {
        value1 = 0,
        listGetCount1,
    } = props;
    console.log('List', props);

    const [count1, setCount1] = useState(value1);

    const handleCount1 = () => {
        setCount1(count1 + 1)
        listGetCount1(count1 + 1)
    }

    return (
        <>

            <ul>
                <li>
                    <Button type="primary" onClick={handleCount1}>
                        Listchange1
                    </Button>
                </li>
            </ul>
        </>
    )
}

export default memo(List,areEqual )

Card/index.jsx子组件

import React, { useState, memo } from 'react';
import Button from '../../../../components/Button';


const areEqual = (prevProps, nextProps) => {
    if (prevProps?.value1 === nextProps?.value1 &&
        prevProps?.value2 === nextProps?.value2) {
            return true
    }
    return false

}
function Card(props) {
    const {
        value1 = 0,
        value2 = 0,
        cardGetCount1,
        cardGetCount2
    } = props;
    console.log('Card', props);
    const [count1, setCount1] = useState(value1);
    const [count2, setCount2] = useState(value2);

    const handleCount1 = () => {
        setCount1(count1 + 1)
        cardGetCount1(count1 + 1)
    }

    const handleCount2 = () => {
        console.log('handleCount2');
        setCount2(count2 + 1)
        cardGetCount2(count2 + 1)
    }
    return (
        <>
            <ul>
                <li>
                    <Button type="primary" onClick={handleCount1}>
                        Cardchange1
                    </Button>
                </li>
                <li>
                    <Button type="primary" onClick={handleCount2}>
                        Cardchange2
                    </Button>
                </li>
            </ul>
        </>
    )
}

export default memo(Card, areEqual)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值