230626 - React(์‡ผํ•‘๋ชฐ)

๋ฐฑ์Šน์—ฐยท2023๋…„ 6์›” 26์ผ
0

๐Ÿšฉ React

๋ฆฌ์•กํŠธ์™€ ํŒŒ์ด์–ด๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‡ผํ•‘๋ชฐ ์‚ฌ์ดํŠธ ๊ตฌํ˜„ํ•˜๊ธฐ 1์ผ์ฐจ

๐Ÿ“ ์„ค๋ช…

  • react์™€ firebase๋ฅผ ์ด์šฉํ•˜์—ฌ ์‡ผํ•‘๋ชฐ ์‚ฌ์ดํŠธ ๊ตฌํ˜„ํ•˜๊ธฐ
  • ์ƒ๋‹จ ํ—ค๋”์™€ ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ”, firebase ๋กœ๊ทธ์ธ ๊ตฌํ˜„


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

App.js

import './App.css';
import { Outlet } from 'react-router-dom';
import Navbar from './components/Navbar';

function App() {
  return (
    <>
      <Navbar />
      <Outlet />
    </>
  );
}

export default App;



index.css

@tailwind base;
@tailwind components;
@tailwind utilities;

@import url("https://fonts.googleapis.com/css2?family=Noto+Sans+KR&family=Playfair+Display&display=swap");

body {
  margin: 0;
  font-family: Noto Sans KR;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;

  @apply text-slate-900;
}

#root {
  @apply w-full text-inherit;
}



index.js

import React from "react";
import ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import "./index.css";
import App from "./App";
import NotFound from "./pages/NotFound";
import Home from "./pages/Home";
import AllProducts from "./pages/AllProducts";
import NewProduct from "./pages/NewProduct";
import ProductDetail from "./pages/ProductDetail";
import MyCart from "./pages/MyCart";

const router = createBrowserRouter([
  {
    path: "/",
    element: <App />,
    errorElement: <NotFound />,
    children: [
      { index: true, path: "/", element: <Home /> },
      { path: "/products", element: <AllProducts /> },
      { path: "/products/new", element: <NewProduct /> },
      { path: "/products/:id", element: <ProductDetail /> },
      { path: "/cart", element: <MyCart /> },
    ],
  },
]);

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <RouterProvider router={router} />
  </React.StrictMode>
);



Navbar.jsx

import React, { useState } from "react";
import { Link } from "react-router-dom";
import { HiPencilAlt } from "react-icons/hi";
import { login, logout } from "../api/firebase";

export default function Navbar() {
  const [user, setUser] = useState();

  // onClick์— login ํ•จ์ˆ˜๋ฅผ ๋„ฃ์ง€ ์•Š๊ณ  ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋Š” ์ด์œ ๋Š” firebase.js์— ์žˆ๋Š” user๋ฅผ ๋ฐ›์•„์™€์„œ useState์— ์ง‘์–ด๋„ฃ๊ธฐ ์œ„ํ•จ
  /** 
   * ๋กœ๊ทธ์ธํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜
   */
  const handleLogin = () => {
    login().then(setUser);
  };
  const handleLogout = () => {
    logout().then(setUser); // useState์˜ user๋ฅผ ๋น„์šด๋‹ค. (null ์ƒํƒœ๋กœ ๋งŒ๋“ฆ)
  };

  return (
    <div className="border-b border-t-slate-300">
      <div className="w-full max-w-screen-2xl m-auto">
        <header className="flex justify-between items-center p-5">
          <h1 className="text-3xl font-logoFont tracking-widest">
            RALPH<span className="pl-3 md:pl-6">LAUREN</span>
          </h1>

          <nav className="flex items-center gap-4">
            <Link to="/products">Product</Link>
            <Link to="/cart">Cart</Link>
            <Link to="/products/new">
              <HiPencilAlt />
            </Link>
            {!user && <button onClick={handleLogin}>login</button>}
            {user && <button onClick={handleLogout}>logout</button>}
          </nav>
        </header>
      </div>
    </div>
  );
}



firebase.js

import { initializeApp } from "firebase/app";
import { getAuth, signInWithPopup, GoogleAuthProvider, signOut } from "firebase/auth";

const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
  projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
  databaseURL: process.env.REACT_APP_FIREBASE_DB_URL,
};

const app = initializeApp(firebaseConfig);
const provider = new GoogleAuthProvider();
const auth = getAuth();

// * ํ•จ์ˆ˜ ์„ ์–ธ์€ firebase ์•ˆ์—์„œ ํ•จ
export function login() {
  return signInWithPopup(auth, provider) // ์•„๋ž˜ ๋ฆฌํ„ด๋œ user ๊ฐ’์„ ๋ฐ›์•„์™€์„œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ ์œ„ํ•ด return ์ž‘์„ฑ
    .then((result) => {
      // ๋กœ๊ทธ์ธ ๋˜์—ˆ๋Š”์ง€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์–ด์˜ด
      const user = result.user;
      // console.log("user? : ", user);
      return user.displayName;
    })
    .catch(console.error);
}

export function logout() {
  return signOut(auth) // null๊ฐ’์ด ๋ฆฌํ„ด๋จ
  .then(() => {}) // null
  // .catch((error) => {});
}



js



์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

  • ์ด๋ฏธ ๋กœ๊ทธ์ธ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋กœ๊ทธ์ธ ์ฐฝ์ด ๋œจ์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋กœ๊ทธ์•„์›ƒ์œผ๋กœ ๋„˜์–ด๊ฐ


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€