代码之家  ›  专栏  ›  技术社区  ›  Darren

React组件中的呈现未返回任何内容

  •  3
  • Darren  · 技术社区  · 6 年前

    index.js 我正在处理的文件重定向为 navigate 基于用户身份验证。

    const IndexAuth = () => navigate(routes.DASHBOARD);
    
    const IndexPageContent = withAuthorization(authCondition)(() => (
     <AuthUserContext.Consumer>
        {authUser => (authUser ? <IndexAuth /> : null)}
      </AuthUserContext.Consumer>
    ));
    
    export default withAuthentication(IndexPage);
    

    IndexAuth routes.DASHBOARD 不渲染。

    IndexAuth(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
    

    据我所知,我需要为组件返回一些东西 render . 我该怎么做?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Joseph Sible-Reinstate Monica    6 年前

    如果IndexAuth只是navigate的一个包装器,那么navigate需要返回一些东西,而实际情况并非如此。如果navigate是您的函数,那么向它添加一个返回值。否则,将其添加到IndexAuth。

        2
  •  0
  •   Darren    6 年前

    只是为了扩展约瑟夫·西布尔的答案,为同一条船上的其他人提供一个解决方案。

    null IndexAuth

    const IndexAuth = () => {
      navigate(routes.DASHBOARD);
      return null;
    };