简体   繁体   English

为什么我的react标头根本不呈现?

[英]why does my react header does not render at all?

So I have aa component called Header that will display a nav bar depending if a user is logged in or not, the props I am passing is a prop from the parent component that returns true as the user is already authenticated after login 因此,我有一个称为Header的组件,该组件将显示导航栏,具体取决于用户是否登录,我传递的道具是来自父组件的道具,由于在登录后用户已通过身份验证,因此返回true

App 应用

 <Header IsLoggedIn={this.state.IsLoggedIn} />  

then this is my Header component that is not rendering 那么这是我的Header组件没有呈现

Header

// stateless functional component
import React from 'react';
import { NavLink } from 'react-router-dom'; //import Navlink to create nav links and to put active class on any link that is active
import UserGreeting from './UserGreeting'
import GuestGreeting from './GuestGreeting'
/*Header- Displays the top menu bar for the application and 
includes buttons for signing in and signing up 
(if there's not an authenticated user) or the user's first and last name 
and a button for signing out (if there's an authenticated user).*/


function Header(props) {
  const isLoggedIn = props;
  console.log(props)
  if (isLoggedIn ===true) {
    return <UserGreeting />;
  }
  else if(isLoggedIn===false) {
    return <GuestGreeting />;
  }
}

export default Header;

this is the error I get when I reload the page: 这是我重新加载页面时遇到的错误:

Uncaught Invariant Violation: Header(...): Nothing was returned from render. 未捕获的不变违规:标头(...):渲染未返回任何内容。 This usually means a return statement is missing. 这通常意味着缺少return语句。 Or, to render nothing, return null. 或者,不渲染任何内容,则返回null。 Can someone help? 有人可以帮忙吗?

If isLoggedIn is undefined then isLoggedIn === false evaluates to false which causes your component to not return anything and results in the error you are seeing. 如果isLoggedIn undefinedisLoggedIn === false评估为false ,这将导致您的组件不返回任何内容,并导致您看到的错误。


Change your else clause to this: 将您的else子句更改为:

else {
  return <GuestGreeting />;
}

...or as @Isaac points out, remove the else completely and just do this: ...或正如@Isaac指出的那样,完全删除else并执行以下操作:

if (isLoggedIn === true) {
  return <UserGreeting />;
}
return <GuestGreeting />;

...and that should fix it. ...那应该解决它。


Ah, and @Jose is correct that you should use destructuring: const { isLoggedIn } = props; 嗯,@ Jose应该使用解构是正确的: const { isLoggedIn } = props; ...that is why isLoggedIn was undefined in the first place. ...这就是为什么首先 undefined isLoggedIn 原因

You should use es6 destructuring: 您应该使用es6解构:

 const { isLoggedIn } = props; 

If you don't destructure your props, you're not getting the correct value from isLoggedIn so you don't return anything (thus the error). 如果不破坏道具,则不会从isLoggedIn获得正确的值,因此您不会返回任何内容(因此将返回错误)。

Also don't need to check if false, just do: 另外也不需要检查是否为假,只需执行以下操作:

 function Header(props) { const { isLoggedIn } = props; if (isLoggedIn) { return <UserGreeting />; } else { return <GuestGreeting />; } } 


TIP: You can do something like this for keeping your code tidy 提示:您可以执行以下操作以保持代码整洁

 const Header = ({ isLoggedIn }) => isLoggedIn ? <UserGreeting /> : <GuestGreeting /> 

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

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