Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Navbar #20

Open
arodriguez47 opened this issue May 25, 2022 · 1 comment
Open

Issue with Navbar #20

arodriguez47 opened this issue May 25, 2022 · 1 comment

Comments

@arodriguez47
Copy link

arodriguez47 commented May 25, 2022

I've been unable to get the Navbar component to work once I add the React Context. I get the following error message:

image

I've tried to refactor the code many ways but still getting a similar message.

This is my latest version:

import Link from 'next/link';
import React from 'react';
import { useContext } from "react";
import { UserContext } from "../lib/context";

const { user, username } = useContext(UserContext)

function LoggedIn() {

    return (
        <>
            <li className='push-left'>
                <Link href="/admin">
                    <button className="btn-blue">Write Posts</button>
                </Link>
            </li>
            <li>
                <Link href={`/${username}`}>
                    <img src={user?.photoURL} alt=/>
                </ Link>
            </li>
        </>
    )
}

function LoggedOut() {
    return (
        <>
            <li>
                <Link href="/enter">
                    <button className="btn-blue">Log in</button>
                </Link>
            </li>
        </>
    )
}

// Top Navbar
export default function Navbar() {


    let loginStatus;
    if (username) {
        loginStatus = <LoggedIn />

    } else {
        loginStatus = <LoggedOut />
    }

    return (
        <nav className='navbar'>
            <ul>
                <li>
                    <Link href="/">
                        <button className={"btn-logo"}>NXT</button>
                    </Link>
                </li>
                {loginStatus}

            </ul>
        </nav>
    )
}

This was my version following the course tutorial...

import Link from 'next/link';
import React from 'react';
import {useContext} from "react";
import {UserContext} from "../lib/context";

// Top Navbar
export default function Navbar() {
    const { user, username } = useContext(UserContext)

    return (
        <nav className='navbar'>
            <ul>
                <li>
                    <Link href="/">
                        <button>FEED</button>
                    </Link>
                </li>

                {/* user is signed in and has username */}
                {username && (
                    <div>
                        <li className='push-left'>
                            <Link href="/admin">
                                <button className="btn-blue">Write Posts</button>
                            </Link>
                        </li>
                        <li>
                            <Link href={`/${username}`}>
                                <img src={user?.photoURL} alt=/>
                            </Link>
                        </li>
                    </div>
                )}


                {/* user is not signed in OR has not created username */}
                {!username && (
                    <li>
                        <Link href="/enter">
                            <button className="btn-blue">Log in</button>
                        </Link>
                    </li>
                )}
            </ul>
        </nav>
    )
}

Neither have worked... I'm using WebStorm

@ivanolmo
Copy link

ivanolmo commented Jun 2, 2022

Not sure if you fixed this yet, but I reproduced your error. The issue is in the second <Link> of your LoggedIn function. The img tag alt attribute has an error. It's currently alt=, so it's pointing to nothing. Change it to something like alt='user' and that should resolve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants