简体   繁体   English

React Component中的渲染没有返回任何内容

[英]Nothing was returned from render in React Component

In the App index.js file I am handling redirects as navigate based on user authentication. 在App index.js文件中,我将重定向作为基于用户身份验证的navigate处理。

const IndexAuth = () => navigate(routes.DASHBOARD);

const IndexPageContent = withAuthorization(authCondition)(() => (
 <AuthUserContext.Consumer>
    {authUser => (authUser ? <IndexAuth /> : null)}
  </AuthUserContext.Consumer>
));

export default withAuthentication(IndexPage);

The redirect works but I am getting an error at IndexAuth and causing the page at routes.DASHBOARD to not render. 重定向的作品,但我在收到错误IndexAuth并导致页面routes.DASHBOARD不渲染。

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

As I understand it, I need to return something for the component to render . 据我了解,我需要返回一些东西供组件render How can I do this? 我怎样才能做到这一点?

If IndexAuth is just a wrapper around navigate, then navigate needs to return something, which it doesn't. 如果IndexAuth只是导航的包装器,那么导航需要返回一些东西,而不是返回东西。 If navigate is your function, then add a return value to it. 如果导航是您的函数,则为其添加返回值。 Otherwise, add it to IndexAuth. 否则,将其添加到IndexAuth。

Just to extend on Joseph Sible's answer with a solution for others in the same boat. 只是为了扩展Joseph Sible的答案,为同一条船上的其他人提供解决方案。

I fixed the issue by returning null in IndexAuth 我通过在IndexAuth返回null来修复此问题

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

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

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