简体   繁体   English

如何防止 React 子组件重新渲染?

[英]How to prevent React Child Component from Re-Rendering?

I am new to React and trying to practice on a personal project but I have this issue.我是 React 的新手并试图在个人项目上练习,但我有这个问题。 I see lots of questions about the same topic but I can not find any similar Q/A related to my issue.我看到很多关于同一主题的问题,但找不到与我的问题相关的任何类似问答。 I have a parent component that I want to render on a specific condition from a redux store.我有一个父组件,我想在 redux 存储中的特定条件下呈现它。 And other two children components {Bookmarks, TOC} that should be rendered on other two related conditions from the same store, the problem is, a third child component {FontAwesomeLicense} renders when the three other conditions change.其他两个子组件 {Bookmarks, TOC} 应该在同一商店的其他两个相关条件下呈现,问题是,当其他三个条件发生变化时,第三个子组件 {FontAwesomeLicense} 会呈现。

Here's the App.js file that have the mentioned components:这是包含上述组件的 App.js 文件:

import Bookmarks from './components/widgets/bookmarks';
import MapInfo from './components/widgets/info';
import MyMap from './components/widgets/map';
import Navbar from './components/ui/navbar';
import { useSelector } from 'react-redux';
import LandingPage from './components/ui/landing';
import TOC from './components/widgets/toc';
import MyToast from './components/ui/toast';
import FontAwesomeLicense from './components/ui/license';
function App() {
    const login = useSelector(state => state.login.isLogged);
    const showBookmark = useSelector(state => state.bookmarks.visibility);
    const showTOC = useSelector(state => state.toc.visibility);
    return (
        <div className="App">
            <Navbar />
            {!login && <LandingPage />}
            {login && <>
                <MyMap />
                <MapInfo />
                <MyToast />
                {showBookmark && <Bookmarks />}
                {showTOC && <TOC />}
            </>}
            <FontAwesomeLicense />
        </div>
    );
};
export default App;

What am I doing wrong here?我在这里做错了什么? I really appreciate any answer.我真的很感激任何答案。 :) :)

当您为 Navbar 和 FontAwesomeLicense 之间的所有内容创建组件时,只有该组件需要重新渲染,因为在父组件中不会检测到任何更改。

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

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