【问题标题】:How to re-render children in ReactJS?如何在 ReactJS 中重新渲染孩子?
【发布时间】:2021-08-22 14:47:40
【问题描述】:

问题背景

我有一个名为<Layout/> 的组件,其中包含我的页眉和页脚。它像这样包装了我所有的路线:

<Layout>
    <div className="container">
        <Route path="/sign-in" exact component={SignIn}/>
        <ProtectedRoute path="/" exact component={Home}/>
        <ProtectedRoute path="/statistics" exact component={Statistics}/>
        ...
        ...
    </div>
</Layout>

我的&lt;Layout/&gt; 组件是这样定义的:

const Layout = (props) => {
    return (
        <div>
            <Header/>
                {props.children}
            <Footer/>
        </div>
    );
}

我这样做是为了不必在每个组件中都包含页眉和页脚。

问题

在我的标题组件中,我使用了auth 实例,它指示用户是否已登录。 auth 在用户登录后发生变化。但是,即使 auth 发生变化,&lt;Layout/&gt; 中的 &lt;Header/&gt; 组件也不会重新呈现。我必须手动刷新它以合并更改。
我的&lt;Header/&gt; 组件定义为:

import auth from '../../auth';

const Header = () => {
    return (
                {auth.isAuth() ? 
                    //render icon A
                : null
                }
                    <div>
                        Healthcare Management System      //header title
                    </div>
                {auth.isAuth() ? 
                    //render icon B
                : null
                }
            </div>
    );
}
export default Header;

这是我的auth 班级:

class Auth{
    constructor(){
        this.authenticated = JSON.parse(sessionStorage.getItem('profile'));
    }
    login(cb){
        this.authenticated = JSON.parse(sessionStorage.getItem('profile'));
        cb();
    }
    logout(cb){
        this.authenticated = null;
        cb();
    }
    isAuth(){
        return this.authenticated;
    }
}
export default new Auth();

我需要什么

我想要的应该很简单;当auth.isAuth() == null 时,不显示图标,只显示标题(这行为正确)。 auth.isAuth() != null 何时显示图标 A 和 B(这行为不正确,需要我刷新页面才能呈现图标)。
不知何故,我希望&lt;Layout/&gt; 组件重新渲染一次auth changes。谢谢!

【问题讨论】:

  • 这不是 HOC。您应该将身份验证作为道具或使用 useEffect
  • @RobertoZvjerković 已编辑!我已经尝试将道具从 Layout 传递到 Header,但它仍然不起作用。
  • 什么是“道具”?您需要回调来指示身份验证状态更改。也就是说,使用上下文或其他状态管理机制可能会更好,这样身份验证状态就可以在整个应用程序中轻松获得。
  • 如何设置是否认证?认证后路径会改变吗?
  • authenticated 逻辑应该处于根组件状态并通过道具传递下来。那,或者使用一些状态管理工具。就个人而言,我只会使用RxJsBehaviorSubject 来保存authenticated 值,然后使用useObservable 钩子从react-use 中提取它。

标签: reactjs render higher-order-components


【解决方案1】:

React 组件仅在其 props 或其状态发生变化时才会重新渲染,因此您应该将 auth 置于可以设置如下的状态:

import auth from '../../auth';

const Layout = (props) => {
    const [authenticated, setAuthenticated] = React.useState(false);
    React.useEffect(() => {
      if(!auth.isAuth()) {
          setAuthenticated(false);      
      } else {
          setAuthenticated(true);      
      }
    }, []);   // this useEffect logic is not working, you need to determine how you want to configure the condition in which authenticated state is set
    return (
        <div>
            <Header authenticated={authenticated} />
                {props.children}
            <Footer/>
        </div>
    );
}

标题组件:

const Header = ({ authenticated }) => {
    return (
                {authenticated ? 
                    //render icon A
                : null
                }
                    <div>
                        Healthcare Management System      //header title
                    </div>
                {authenticated ? 
                    //render icon B
                : null
                }
            </div>
    );
}
export default Header;

【讨论】:

  • 还是不行。我什至控制台在布局组件中记录了authenticated 状态,它仍然只是返回最初设置的状态,直到我刷新。
  • 我进行了编辑以显示身份验证文件。对此有何建议?
  • 您提到“用户登录后auth更改”,但我在您的auth类中没有看到这个逻辑,您如何设置会话存储以指示用户已登录?
猜你喜欢
  • 2016-07-26
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
相关资源
最近更新 更多