类型错误:无法读取未定义的属性“isAuthenticated”

时间:2021-04-26 18:15:10

标签: reactjs redux mern

我正在尝试修复以下错误:

TypeError: 无法读取未定义的属性“isAuthenticated” 导航栏 E:/smnd/client/src/components/layout/Navbar.js:7

   import { logout } from '../../actions/auth';
   
 const Navbar = ({ auth: { isAuthenticated, loading }, logout }) => {

这是我的导航栏代码,我已经定义了我的 isAuthenticated 我已经在 auth.js reducers 文件中定义了它......中间件工作不正常


import React, { Fragment } from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { logout } from '../../actions/auth';

const Navbar = ({ auth: { isAuthenticated, loading }, logout }) => {
  const authLinks = (
    <ul>
      <li>
      <a onClick={logout} href='#!'>
        <i className="fas fa-sign-out-alt"></i>{' '}
        <span className="hide-sm">Logout</span> 
      </a></li>
      {/* <li>
        <Link to='/profiles'>Developers</Link>
      </li>
      <li>
        <Link to='/dashboard'>
          <i className='fas fa-user' />{' '}
          <span className='hide-sm'>Dashboard</span>
        </Link>
      </li>
      <li>
        <a onClick={logout} href='#!'>
          <i className='fas fa-sign-out-alt' />{' '}
          <span className='hide-sm'>Logout</span>
        </a>
      </li> */}
    </ul>
  );

  const guestLinks = (
    <ul>
      <li>
        <Link to='#!'>Developers</Link>
      </li>
      <li>
        <Link to='/register'>Register</Link>
      </li>
      <li>
        <Link to='/login'>Login</Link>
      </li>
    </ul>
  );

  return (
    <nav className='navbar bg-dark'>
      <h1>
        <Link to='/'>
          <i className='fas fa-code' /> DevConnector
        </Link>
      </h1>
      {!loading && (
        <Fragment>{isAuthenticated ? authLinks : guestLinks}</Fragment>
      )}
    </nav>
  );
};

Navbar.propTypes = {
  logout: PropTypes.func.isRequired,
  auth: PropTypes.object.isRequired
};

const mapStateToProps = state => ({
  auth: state.auth
});

export default connect(
  mapStateToProps,
  { logout }
)(Navbar);

0 个答案:

没有答案