๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/React

[React] ์‡ผํ•‘๋ชฐ ๋ ˆ์ด์•„์›ƒ ๋””์ž์ธํ•˜๊ธฐ

by Connecting-the-dots 2022. 3. 23.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

๐Ÿ’œ ์‡ผํ•‘๋ชฐ ๋ ˆ์ด์•„์›ƒ PC ํ™”๋ฉด


๐Ÿ’œ ์‡ผํ•‘๋ชฐ ๋ ˆ์ด์•„์›ƒ ๋ชจ๋ฐ”์ผ ํ™”๋ฉด

 


๐Ÿ’œ Navbar ๋งŒ๋“ค์–ด๋ณด๊ธฐ

๐Ÿค React Bootstrap ์—์„œ Navbar Components ๊ฒ€์ƒ‰ํ•˜๊ธฐ

  • React Bootstrap ๊ณต์‹์‚ฌ์ดํŠธ๋กœ ๋“ค์–ด๊ฐ€์„œ Components ๋ฉ”๋‰ด์—์„œ navbar ๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•˜๋ฉด ์ƒ˜ํ”Œ๋“ค์ด ๋งŽ์ด ๋‚˜์˜จ๋‹ค.
  • ๊ทธ๋Ÿฌ๋ฉด ๋งˆ์Œ์— ๋“œ๋Š” navbar ์˜ HTML ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ JavaScript ํŒŒ์ผ์— ๋ถ™์—ฌ๋„ฃ์–ด์ฃผ๋ฉด ๋””์ž์ธ์€ ๋!
  • ์„ธ๋ถ€ ๋‚ด์šฉ์ด๋‚˜ ๋””์ž์ธ๋งŒ ์†๋ณด๋ฉด ์˜ˆ์œ Navbar ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๐Ÿค ์›ํ•˜๋Š” ๋””์ž์ธ์˜ Navbar HTML ๋ณต์‚ฌ-๋ถ™์—ฌ๋„ฃ๊ธฐ

 

function App() {

  return (
    <div className="App">
      <Navbar bg="light" expand="lg">
        <Container>
          <Navbar.Brand href="#home">Shoes shop</Navbar.Brand>
          <Navbar.Toggle aria-controls="basic-navbar-nav" />
          <Navbar.Collapse id="basic-navbar-nav">
            <Nav className="me-auto">
              <Nav.Link href="#home">Home</Nav.Link>
              <Nav.Link href="#link">Link</Nav.Link>
              <NavDropdown title="Dropdown" id="basic-nav-dropdown">
                <NavDropdown.Item href="#action/3.1">Action</NavDropdown.Item>
                <NavDropdown.Item href="#action/3.2">Another action</NavDropdown.Item>
                <NavDropdown.Item href="#action/3.3">Something</NavDropdown.Item>
                <NavDropdown.Divider />
                <NavDropdown.Item href="#action/3.4">Separated link</NavDropdown.Item>
              </NavDropdown>
            </Nav>
          </Navbar.Collapse>
        </Container>
      </Navbar>
    </div>
  );
}
  • ๋‚˜๋Š” React Bootstrap ์—์„œ Navbar ๊ฒ€์ƒ‰ ์‹œ ๋งจ ์ฒ˜์Œ์— ๋‚˜์˜ค๋Š” HTML ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ App.js ํŒŒ์ผ ๋‚ด ์›ํ•˜๋Š” ์œ„์น˜์— ๋ถ™์—ฌ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

๐Ÿค Navbar Component import ํ•˜๊ธฐ

import {Navbar, Container, Nav, NavDropdown} from 'react-bootstrap';
  • ์ด ๋•Œ ์ค‘์š”ํ•œ ์ ์€ ๋ถ™์—ฌ๋„ฃ์€ Component ๋ฅผ JavaScript ํŒŒ์ผ ์ƒ๋‹จ์— import ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
  • import ํ•ด์ค„ ๋•Œ์—๋Š” <Navbar /> ๋ง๊ณ ๋„ ํ•˜์œ„์— ํฌํ•จ๋˜์–ด์žˆ๋Š” <Nav />, <Container /> ๋“ฑ์˜ Component ๋„ import ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๐Ÿค ๋ธŒ๋ผ์šฐ์ €์—์„œ Navbar ์ ์šฉ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ

  • ์ด์ œ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹ฌํ”Œํ•œ Navbar ๊ฐ€ ์™„์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’œ ๋Œ€๋ฌธ Jumbotron ๋งŒ๋“ค๊ธฐ

  • Jumbotron ์ด๋ผ๊ณ  ํ•ด์„œ ๋Œ€๋ฌธ ์ด๋ฏธ์ง€๋„ React Bootstrap ์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ํ•˜์ง€๋งŒ bootstrap5, React Bootstrap 2 ๋ฒ„์ „ ์ด์ƒ์—์„œ๋Š” Jumbotron ์ด๋ผ๋Š” ํ•ญ๋ชฉ์ด ๊ฒ€์ƒ‰๋˜์ง€ ์•Š๋Š”๋‹ค.
  • ๊ทธ๋ƒฅ ํ•œ๋ฒˆ ๋งŒ๋“ค์–ด๋ณด๋Š” ๊ฒƒ๋„ ์žฌ๋ฏธ์žˆ์„ ๊ฑฐ ๊ฐ™์•„์„œ ์ง์ ‘ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.

๐Ÿค HTML

<div className="main-visual">
    <h2>Welcome to visit SHOES.COM!</h2>
    <hr/>
    <h1>20% Season Off</h1>
    <button>SHOP THIS DEAL</button>
</div>
  • ์œ„ ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ๋Œ€๋ฌธ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•˜๋‹ค.
  • ์ฒ˜์Œ์—๋Š” div ํƒœ๊ทธ ์•ˆ์— ์ด๋ฏธ์ง€ ๋งํฌ๋ฅผ ๋‹ด์€ img ํƒœ๊ทธ์™€ ๋‚ด์šฉ์ด ๋“ค์–ด์žˆ๋Š” div ํƒœ๊ทธ๋ฅผ ๋„ฃ์–ด์ค€ ํ›„, position: absolute ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ์œ„์— ๊ธ€์ด ํ•œ๊ฐ€์šด๋ฐ์— ์˜ค๋„๋ก ์กฐ์ •ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.
  • ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ์œ„ html ์ฝ”๋“œ์™€ ๊ฐ™์ด main-visual ์ด๋ผ๋Š” className ์„ ๊ฐ€์ง„ div ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด ๊ทธ ์•ˆ์— ๋‚ด์šฉ์„ ๋ชจ๋‘ ๋‹ด์•„์ฃผ๊ณ  ๋ฐฐ๊ฒฝ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด๋ณด์•˜๋‹ค.

๐Ÿค CSS

.main-visual {
	background-image: url("../src/img/shoes.jpg");
	background-size: cover;
	background-position: center;
	color: white;
	padding: 20px 30px;
	min-height: 300px;
}

.main-visual h2 {
	margin: 30px 0;
}

.main-visual button {
	margin-top: 20px;
	border: none;
	border-radius: 5px;
	padding: 10px 20px;
	background: red;
	color: white;
	font-weight: bold;
	font-size: 14px;
}

.main-visual button:hover {
	color: red;
	background: white;
	transition: all 0.5s;
	transform: scale(1.05);
}
  • ์ด๋ฏธ์ง€๋ฅผ ๋ฐฐ๊ฒฝ์œผ๋กœ ๋„ฃ์„ ๋•Œ ์œ ์šฉํ•œ css ์†์„ฑ๋“ค์ด ๋ช‡ ๊ฐœ ์žˆ๋‹ค.
    • background-image : url('์ด๋ฏธ์ง€ ๊ฒฝ๋กœ') ๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋ฐฐ๊ฒฝ์œผ๋กœ ๋„ฃ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์†์„ฑ์œผ๋กœ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ๋ฅผ ๋”ฐ์˜ดํ‘œ ์•ˆ์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.
    • background-size : cover ๋Š” ๋ฐฐ๊ฒฝ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฏธ์ง€๋ฅผ ํƒœ๊ทธ ์•ˆ์— ๊ฝ‰ ์ฐจ๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์†์„ฑ์ด๋‹ค.
    • background-position : center ๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ๋„ˆ๋น„๋ฅผ ์กฐ์ •ํ•  ๋•Œ ์ด๋ฏธ์ง€์˜ ์ค‘์•™๋ถ€๊ฐ€ ๊ฐ€์šด๋ฐ์— ์˜ค๋„๋ก ํ•ด์ฃผ๋Š” ์†์„ฑ์ด๋‹ค.

๐Ÿค ๋ธŒ๋ผ์šฐ์ €์—์„œ Navbar ์ ์šฉ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ


๐Ÿ’œ ์ƒํ’ˆ ๋ ˆ์ด์•„์›ƒ ๋งŒ๋“ค๊ธฐ

  • ์ƒํ’ˆ ์ด๋ฏธ์ง€๋ฅผ ์ง„์—ดํ•˜๋Š” ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค ๋•Œ์—๋„ Bootstrap ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋น ๋ฅด๊ฒŒ ๋””์ž์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿค HTML

  <div className="container">
    <div className="row">
      <div className="col-md-4 grid">
        <img src="https://codingapple1.github.io/shop/shoes1.jpg" />
        <h5>White and Black</h5>
        <p>120000</p>
      </div>
      <div className="col-md-4 grid">
        <img src="https://codingapple1.github.io/shop/shoes2.jpg" />
        <h5>Red Knit</h5>
        <p>110000</p>
      </div>
      <div className="col-md-4 grid">
        <img src="https://codingapple1.github.io/shop/shoes3.jpg" />
        <h5>Grey Yordan</h5>
        <p>130000</p>
      </div>
    </div>
  </div>
  • container, row, col-md-4 ๋ผ๋Š” className ์€ Bootstrap ๋ฌธ๋ฒ•์— ํ•ด๋‹นํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ, ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋ณ„๋„์˜ css ์†์„ฑ์„ ์ •์˜ํ•˜์ง€ ์•Š๊ณ ๋„ ๊ฐ€๋กœ 1์ค„์„ ๋™์ผํ•œ ํฌ๊ธฐ๋กœ 3๋ถ„ํ• ํ•ด์ฃผ๊ณ  ๋ชจ๋ฐ”์ผ์—์„œ๋Š” ์„ธ๋กœ 1์ค„์„ ๋™์ผํ•œ ํฌ๊ธฐ๋กœ 3๋ถ„ํ• ํ•ด์ค€๋‹ค.

๐Ÿค CSS

.grid img {
	width: 100%;
}
  • ๋ธŒ๋ผ์šฐ์ €๋กœ ๋ถˆ๋Ÿฌ์˜จ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ์ปค์„œ width ๋ฅผ 100% ๋กœ ๋งž์ถ”์–ด ๊ฐ ์˜์—ญํฌ๊ธฐ์— ๊ฝ‰ ์ฐจ๋„๋ก css ๋ฅผ ์ˆ˜์ •ํ•ด์ฃผ์—ˆ๋‹ค.

๐Ÿค ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ƒํ’ˆ ๋ ˆ์ด์•„์›ƒ ์ ์šฉ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ

 

728x90
๋ฐ˜์‘ํ˜•