skip to Main Content

how do i get these providers?

so i was following the javascript mastery tutorial and i did the exact same thing , but this useEffect to get the providers isnt working ( i tried alerting it to see it keeps showing null)

here’s the piece of code of the providers , tell me what could be wrong

const [providers, setProviders] = useState(null);
 const [toggleDropdown, setToggleDropdown] = useState(false);

 useEffect(() => {
   const setUpProviders = async () => {
     const response = await getProviders();
     setProviders(response);
   };

   setUpProviders();
 }, []);

2

Answers


  1. Chosen as BEST ANSWER

    here is the full code :

        "use client";
    
    import Link from "next/link";
    import Image from "next/image";
    import { useState, useEffect } from "react";
    import { signIn, signOut, useSession, getProviders } from "next-auth/react";
    const Nav = () => {
      const { data: session } = useSession();
      const [providers, setProviders] = useState(null);
      const [toggleDropdown, setToggleDropdown] = useState(false);
    
      useEffect(() => {
        (async () => {
          const res = await getProviders();
          setProviders(res);
        })();
      }, []);
    
      useEffect(() => {
        alert(providers);
      }, [providers]);
    
      return (
        <nav className="flex-between w-full mb-16 pt-3">
          <Link href="/" className="flex gap-2 flex-center">
            <Image
              src="/assets/images/logo.svg"
              alt="Promptopia logo"
              width={30}
              height={30}
              className="object-contain"
            />
            <p className="logo_text">Promptopia</p>
          </Link>
          <div className="sm:flex hidden">
            {session?.user ? (
              <div className="flex gap-3 md:gap-5">
                <Link href="/create-prompt" className="black_btn">
                  Create Post
                </Link>
    
                <button type="button" onClick={signOut} className="outline_btn">
                  Sign Out
                </button>
    
                <Link href="/profile">
                  <Image
                    src="/assets/images/logo.svg"
                    width={37}
                    height={37}
                    className="rounded-full"
                    alt="profile"
                  />
                </Link>
              </div>
            ) : (
              <>
                {providers &&
                  Object.values(providers).map((provider) => (
                    <button
                      type="button"
                      key={provider.name}
                      onClick={() => signIn(provider.id)}
                      className="black_btn"
                    >
                      Sign In
                    </button>
                  ))}
              </>
            )}
          </div>
          <div className="sm:hidden flex relative">
            {session?.user ? (
              <div className="flex">
                <Image
                  src="/assets/images/logo.svg"
                  width={37}
                  height={37}
                  className="rounded-full"
                  alt="profile"
                  onClick={() => setToggleDropdown((prev) => !prev)}
                />
    
                {toggleDropdown && (
                  <div className="dropdown">
                    <Link
                      href="/profile"
                      className="dropdown_link"
                      onClick={() => setToggleDropdown(false)}
                    >
                      My Profile
                    </Link>
                    <Link
                      href="/create-prompt"
                      className="dropdown_link"
                      onClick={() => setToggleDropdown(false)}
                    >
                      Create Prompt
                    </Link>
                    <button
                      type="button"
                      onClick={() => {
                        setToggleDropdown(false);
                        signOut();
                      }}
                      className="mt-5 w-full black_btn"
                    >
                      Sign Out
                    </button>
                  </div>
                )}
              </div>
            ) : (
              <>
                {providers &&
                  Object.values(providers).map((provider) => (
                    <button
                      type="button"
                      key={provider.name}
                      onClick={() => signIn(provider.id)}
                      className="black_btn"
                    >
                      Sign In
                    </button>
                  ))}
              </>
            )}
          </div>
        </nav>
      );
    };
    
    export default Nav;
    

  2. This is expected behaviour of useState hook. You can use useEffect to check when your variable updates.

    const [providers, setProviders] = useState(null);
    const [toggleDropdown, setToggleDropdown] = useState(false);
    
    useEffect(() => {
      const setUpProviders = async () => {
        const response = await getProviders();
        setProviders(response);
      };
    
      setUpProviders();
    }, []);
    
    useEffect(() => {
      console.log(providers)
    }, [providers])
    

    For detailed explanation of this behaviour, check this

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search