๐Ÿš€SPA Webpage ๋งŒ๋“ค์–ด๋ณด๊ธฐ Part 1. Navbar(2)

๐Ÿ”ํ–„๋ฒ„๊ฑฐ ์•„์ด์ฝ˜ ๋„ฃ๊ธฐ with ์‚ผํ•ญ ์—ฐ์‚ฐ์ž

ํ‰ํ™”์˜ ์ƒ์ง• ๋น„๋‘˜๊ธฐ ์•„์ด์ฝ˜์„ ๋„ฃ์–ด์ฃผ์—ˆ์œผ๋‹ˆ ๊ทธ ๋‹ค์Œ์€ ํ–„๋ฒ„๊ฑฐ ์•„์ด์ฝ˜์„ ๋„ฃ์—ˆ๋‹ค.

์™œ ๊ทธ๋Ÿฐ๊ฑฐ ์žˆ์ง€ ์•Š๋‚˜. ๋ˆ„๋ฅด๋ฉด ๋ฉ”๋‰ดํŒ ๋งˆ๋ƒฅ ์ดค๋ฝ~ ํŽผ์ณ์ง€๋Š” ๊ฑฐ

์•„ ํ–„๋ฒ„๊ฑฐ ๋จน๊ณ  ์‹ถ๋‹ค..

์—ฌํŠผ Navbar 1ํŽธ์— ์ด์–ด์„œ..

https://dev-seolleung2.netlify.app/development/Self-intro-app-1/

๋‚ด ๋ฉ”๋‰ดํŒ์„ ๋ณด์—ฌ์ค„ ํ–„๋ฒ„๊ฑฐ๋ฅผ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๋„ฃ์—ˆ๋‹ค.

<div className="menu-icon">
  <i className={click ? 'fas fa-times' : 'fas fa-bars'} />
</div>

click ์ด true ์ด๋ฉด, โ€˜fas fa-timesโ€™ ์ฆ‰ X ์•„์ด์ฝ˜์„ ๋ณด์—ฌ์ฃผ๊ณ ,

์•„๋‹ˆ๋ฉด (ํด๋ฆญ ์•ˆํ–ˆ์„ ๋–„) ํ–„๋ฒ„๊ฑฐ ์•„์ด์ฝ˜์„ ๋ณด์—ฌ์ฃผ๊ฒ ๋‹จ ๊ฑฐ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ํด๋ฆญ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•ด์•ผ ํ•˜๋Š”๋ฐ ํ•ด์„œ ์—ฌ๊ธฐ์— onClick ์ด๋ฒคํŠธ๋ฅผ ๊ฑธ์–ด์ฃผ๋Š” ๊ฑฐ๋‹ค.

<div className="menu-icon" onClick={handleClick}>
  <i className={click ? 'fas fa-times' : 'fas fa-bars'} />
</div>

๊ทธ๋ฆฌ๊ณ  handleClick ์„ ์„ ์–ธํ•ด ์ค€๋‹ค,.

const handleClick = () => setClick(!click)

handleClick ์„ ํ†ตํ•ด์„œ click ์˜ state ๋ฅผ ๋ฐ”๊ฟ” ์ค„ ์ˆ˜ ์žˆ๋Š” setClick ํ•จ์ˆ˜๊ฐ€ ๋ฐœ๋™ํ•œ๋‹ค.

click ์˜ default state ๋Š” false ๋กœ ์„ค์ •ํ–ˆ์—ˆ์œผ๋‹ˆ๊นŒ !click ์„ ํ†ตํ•ด ์ด์ œ click ์˜ ์ƒํƒœ๋ฅผ true ๋กœ ๋ฐ”๊พธ๊ฒ ๋‹จ ๊ฑฐ๋‹ค!

์ฆ‰ ํด๋ฆญ์„ ํ–ˆ์–ด? ๊ทธ๋Ÿผ ๋‚œ X ํ‘œ ๋ณด์—ฌ์ค„๊บผ์ž„.

fa times icon

ํฌ.. ์ด์ œ ํ–„๋ฒ„๊ทธ ์•„์ด์ฝ˜๋„ ๋งŒ๋“ค๊ณ , onClick ์ด๋ฒคํŠธ๋กœ ์•„์ด์ฝ˜์ด ๋ฐ”๋€Œ์–ด์ง€๊ฒŒ๋„ ํ•ด์คฌ๋‹ค!

hamburger icon

๐Ÿ”ํ–„๋ฒ„๊ฑฐ์™€ ์—ฐ๋™ํ•  Nav ๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ธฐ.

์ผ๋‹จ ์ „์ฒด ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด ๋ณด์ž.

import React, { useState } from 'react'
import { Link } from 'react-router-dom'

function Navbar() {
  const [click, setClick] = useState(false)
  const handleClick = () => setClick(!click)
  const closeMobileMenu = () => setClick(false)

  return (
    <>
      <nav className="navbar">
        <div className="navbar-container">
          <Link to="/" className="navbar-logo">
            Dev.SL2 <i className="fas fa-dove" />
          </Link>
          <div className="menu-icon" onClick={handleClick}>
            <i className={click ? 'fas fa-times' : 'fas fa-bars'} />
          </div>
          <ul className={click ? 'nav-menu active' : 'nav-menu'}>
            <li className="nav-item">
              <Link to="/" className="nav-links" onClick={closeMobileMenu}>
                Home
              </Link>
            </li>
          </ul>
        </div>
      </nav>
    </>
  )
}

๊ธฐ์กด ์ฝ”๋“œ์—์„œ ์ถ”๊ฐ€ ๋œ ๊ฒƒ์€ ๋ฐ”๋กœ ul ํƒœ๊ทธ ์ด๋‹ค.

ul ์—๋„ click ์˜ state ๊ฐ€ true ์ผ ๋•Œ โ€˜nav-menu activeโ€™ ๋กœ className ์ด ๋ฐ”๋€Œ๊ฒŒ ํ•ด ์ฃผ์—ˆ๋‹ค.

์ด๊ฑด ๋‚˜์ค‘์— css ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

<ul className={click ? 'nav-menu active' : 'nav-menu'}>
  <li className="nav-item">
    <Link to="/" className="nav-links" onClick={closeMobileMenu}>
      Home
    </Link>
  </li>
</ul>

li ํƒœ๊ทธ ๋‚ด์— Link ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  onClick ์ด๋ฒคํŠธ๋กœ closeMobileMenu ํ•จ์ˆ˜๋ฅผ ๊ฑธ์–ด ์ฃผ์—ˆ๋‹ค.

๋ณ€์ˆ˜๋ช…์„ ๋ช…๊ธฐ ํ•  ๋•Œ ์˜๋ฏธ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ „๋‹ฌ๋˜๋Š” ์ด๋ฆ„์„ ์ง€์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฑธ ๊นจ๋‹ซ๊ณ  ์žˆ๋‹ค.

์ด์ œ closeMobileMenu ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์•ผ์ง€!

const closeMobileMenu = () => setClick(false)

closeMobileMenu ํ•จ์ˆ˜๋Š” click ์˜ status ๋ฅผ false ๋กœ ๋งŒ๋“ ๋‹ค.

click ์˜ status ๊ฐ€ false ๋กœ ๋ฐ”๋€Œ๋ฉด, ul ์— ๊ฑธ์€ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž์— ์˜ํ•ด์„œ className ์ด โ€˜nav-menuโ€™ ์ธ ์ƒํƒœ๋ฅผ ๋ณด์—ฌ์ค„ ๊ฒƒ์ด๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๊ณ  ํ™”๋ฉด์„ ๋ณด๋ฉด ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๐ŸฅณApp.js ์— import BrowserRouter ํ•˜๊ธฐ

์ด ๋ถ€๋ถ„์€ ๋‚ด์ผ ์‹œ์ž‘ ์ „ ์ฐพ์•„๋ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.

import React from 'react'
import Navbar from './components/Navbar'
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'
import './App.css'

function App() {
  return (
    <>
      <Router>
        <Navbar />
        <Switch>
          <Route path="/" exact />
        </Switch>
      </Router>
    </>
  )
}

export default App

๋ญ”๊ฐ€ ํ†ต๋ฐ• ๋•Œ๋ ค ๋งž์ถ”๊ธฐ(?) ๋กœ ์ƒ๊ฐํ•ด ๋ณด๋ฉด,

์ €๊ฒƒ๋“ค์„ ๋ถˆ๋Ÿฌ ์™€์•ผ route ๊ฒฝ๋กœ๋ฅผ โ€˜react-router-domโ€™ ์—์„œ ๋ถˆ๋Ÿฌ์˜จ ์ปดํฌ๋„ŒํŠธ์™€ ํ•ฉ์„? ์ด๋ฃจ๋Š” ๊ฑฐ ๊ฐ™๋‹ค.

์ผ๋‹จ ๋ฐ›์•„๋“ค์ด์ž.

์ด์ œ li ๋ชฉ๋ก์„ ๋” ๋„ฃ์–ด ์ฃผ์—ˆ๋‹ค. ์ด 4๊ฐœ!

navbar

์ตœ์ข… ์™„์„ฑ๋œ ์ฝ”๋“œ

import React, { useState } from 'react'
import { Link } from 'react-router-dom'

function Navbar() {
  const [click, setClick] = useState(false)
  const handleClick = () => setClick(!click)
  const closeMobileMenu = () => setClick(false)

  return (
    <>
      <nav className="navbar">
        <div className="navbar-container">
          <Link to="/" className="navbar-logo">
            Dev.SL2 <i className="fas fa-dove" />
          </Link>
          <div className="menu-icon" onClick={handleClick}>
            <i className={click ? 'fas fa-times' : 'fas fa-bars'} />
          </div>
          <ul className={click ? 'nav-menu active' : 'nav-menu'}>
            <li className="nav-item">
              <Link to="/" className="nav-links" onClick={closeMobileMenu}>
                Home
              </Link>
            </li>
            <li className="nav-item">
              <Link
                to="/services"
                className="nav-links"
                onClick={closeMobileMenu}
              >
                Introduction
              </Link>
            </li>
            <li className="nav-item">
              <Link
                to="/products"
                className="nav-links"
                onClick={closeMobileMenu}
              >
                Skills
              </Link>
            </li>
            <li className="nav-item">
              <Link
                to="/sign-up"
                className="nav-links-mobile"
                onClick={closeMobileMenu}
              >
                Sign-up
              </Link>
            </li>
          </ul>
        </div>
      </nav>
    </>
  )
}

export default Navbar

์™€์šฐ~~! ๋ฉ‹์ ธ๋ฉ‹์ ธ!๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ๐Ÿฅณ


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook