简体   繁体   English

使用 React.memo 防止重新渲染

[英]Preventing rerenders with React.memo

I have built a web app where users have profiles with different pages.我构建了一个 web 应用程序,其中用户的配置文件具有不同的页面。 In all the pages there is a profile picture and a banner and I want them to be rendered only once.在所有页面中都有个人资料图片和横幅,我希望它们只呈现一次。

Now, every time the user changes the page the profile picture and banner are refreshed.现在,每次用户更改页面时,个人资料图片和横幅都会刷新。 I want to stop this.我想阻止这一切。

How it works now:现在如何运作:

现在如何运作

What I want:我想要的是:

我想要的是

I have been playing with React.memo but I can't make it work.我一直在玩 React.memo 但我无法让它工作。

Every parent page (Presets, Store and Courses) fetch all the data (profile picture, banner...).每个父页面(预设、商店和课程)获取所有数据(个人资料图片、横幅......)。

And then I pass the data to the child component Banner and ProfilePicture.然后我将数据传递给子组件 Banner 和 ProfilePicture。

This is my Banner component code (the banner prop is an url type string):这是我的Banner 组件代码(banner 属性是一个 url 类型的字符串):

import React, { useState, useEffect } from 'react';
import { Container, Row, Col} from "react-bootstrap";

const Banner = ({banner}) => {

const [width, setWidth] = useState(window.innerWidth);
const breakpoint = 620;


useEffect(() => {
    window.addEventListener("resize", () => setWidth(window.innerWidth));
},[])

return ( 
    <>
        <Container style={{ maxWidth: "100%" }}>
            <Row>
                <Col className="pl-0 pr-0">
                    {width < breakpoint ?
                        <div
                        style={{
                            backgroundImage: `url(${banner})`,
                            backgroundSize: "cover",
                            backgroundPosition: "center center",
                            height: "40vw",
                            display: "flex",
                            alignItems: "center",
                        }}
                        className="justify-content-center"
                        >
                        
                        </div>
                    :    
                        <div
                        style={{
                            backgroundImage: `url(${banner})`,
                            backgroundSize: "cover",
                            backgroundPosition: "center center",
                            height: "25vw",
                            display: "flex",
                            alignItems: "center",
                        }}
                        className="justify-content-center"
                        >
                        
                        </div>
                    }
                </Col>
            </Row>
        </Container>
    </>
 );
}

export default React.memo(Banner);

And in the parent components (All the pages) I fetch the data and I pass it to the banner component like this:父组件(所有页面)中,我获取数据并将其传递给横幅组件,如下所示:

<Banner banner={banner}/>

I think banner is first undefined and then is the fetched url so the props change every time from undefined to string and thats why React memo is not working.我认为横幅首先是未定义的,然后是获取的 url,所以道具每次都会从未定义更改为字符串,这就是 React 备忘录不起作用的原因。

And I don't know how to solve this.而且我不知道如何解决这个问题。

I have solved my own question.我已经解决了我自己的问题。 I'm not sure this is the best answer but this is what I did:我不确定这是最好的答案,但这是我所做的:

With react-router I have added this route: /:username/:page使用 react-router 我添加了这条路线:/:用户名/:页面

Then I access the page name with props.match.params.page and change the child components depending on it.然后我使用 props.match.params.page 访问页面名称并根据它更改子组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM