[Node+React] Page ์ƒ์„ฑ

poseassomeยท2022๋…„ 1์›” 22์ผ
0

Node+React

๋ชฉ๋ก ๋ณด๊ธฐ
2/2
post-thumbnail

์ƒํ’ˆ ์—…๋กœ๋“œ ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ

(1) ๋น„์–ด ์žˆ๋Š” ์—…๋กœ๋“œ ํŽ˜์ด์ง€ ์ƒ์„ฑ

ํ”„๋กœ์ ํŠธ ํด๋”์—์„œ ํŽ˜์ด์ง€๋ฅผ ์‹คํ–‰ํ•œ๋‹ค npm run dev

๐Ÿ“client
โ”œโ”€โ”€ ๐Ÿ“src
โ”‚   โ”œโ”€โ”€ ๐Ÿ“component     // ๋ชจ๋“  component๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” ํด๋” 
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“view      // Page๋“ค ์กด์žฌ
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“UploadProductPageโญ๏ธ          // ์—…๋กœ๋“œ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค ํด๋” ์ƒ์„ฑ
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ UploadProductPage.jsโญ๏ธ     // ์—…๋กœ๋“œ ํŽ˜์ด์ง€
โ”‚   โ”‚   โ””โ”€โ”€ App.js
โ”‚   โ”‚   โ””โ”€โ”€ Config.js   

UploadProductPage.js์—์„œ rfce๋กœ functional component๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

(2) ์—…๋กœ๋“œ ํŽ˜์ด์ง€ Route ๋งŒ๋“ค๊ธฐ

App.js์—์„œ ์ž‘์„ฑ

import UploadProductPage from './views/UploadProductPage/UploadProductPage';

๋งŒ๋“  ์—…๋กœ๋“œ ํŽ˜์ด์ง€ component๋ฅผ importํ•˜๊ณ 
importํ•œ component๋ฅผ Route component์— ์ถ”๊ฐ€ํ•œ๋‹ค.

<Route exact path="/product/upload" component={Auth(UploadProductPage, true)} />

UploadProductPage๋Š” ๋กœ๊ทธ์ธํ•œ ์‚ฌ๋žŒ๋งŒ ์ ‘๊ทผํ•ด์•ผ๋˜๋ฏ€๋กœ true๋กœ ์„ค์ •ํ•œ๋‹ค.
๊ฒฝ๋กœ๋Š” ์›ํ•˜๋Š” ๋Œ€๋กœ ์ž‘์„ฑํ•œ๋‹ค.

(3) ์—…๋กœ๋“œ ํŽ˜์ด์ง€ ํƒญ ๋งŒ๋“ค๊ธฐ

์šฐ์„  ํŽ˜์ด์ง€์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ํšŒ์›๊ฐ€์ž… ํ›„ ๋กœ๊ทธ์ธ์„ ํ•œ๋‹ค.
component > views > NavBar > Section > RightMenu.js
๋กœ๊ทธ์ธ ํ›„ ๋ณด์ด๋Š” ํƒญ ๋ฉ”๋‰ด ์•„์ด์ฝ˜์— Upload ํƒญ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

(4) Drop Zone์„ ์ œ์™ธํ•œ Form ๋งŒ๋“ค๊ธฐ

antd๋ผ๋Š” CSS ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ <form>์„ ๋งŒ๋“ ๋‹ค.

import React from 'react'
import { Typography, Button, Form, Input } from 'antd';

const { TextArea } = Input;

function UploadProductPage() {
  return (
    <div style={{ maxWidth: '700px', margin: '2rem auto' }}>
      <div style={{ textAlign: 'center', marginBottom: '2rem' }}>
        <h2 level={2}>์—ฌํ–‰ ์ƒํ’ˆ ์—…๋กœ๋“œ</h2>
      </div>
      <Form>
        {/* DropZone */}
        <br />
        <br />
        <label>์ด๋ฆ„</label>
        <Input />
        <br />
        <br />
        <label>์„ค๋ช…</label>
        <TextArea />
        <br />
        <br />
        <label>๊ฐ€๊ฒฉ($)</label>
        <Input />
        <br />
        <br />
        <select>
          <option></option>
        </select>
        <br />
        <br />
        <Button>ํ™•์ธ</Button>

      </Form>
    </div>
  )
}

export default UploadProductPage

ํŒŒ์ผ ์—…๋กœ๋“œ๋งŒ์„ ์œ„ํ•œ component ๋งŒ๋“ค๊ธฐ

(5) ๋ชจ๋“  INPUT์„ ์œ„ํ•œ onChange Function ๋งŒ๋“ค๊ธฐ

input์˜ value๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ์œ„ํ•ด component์— onChange event๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  value์— state๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

const [Title, setTitle] = useState("");
const titleChangeHandler = (event) => {
  setTitle(event.currentTarget.value);
}

return{
  ...
    <Input onChange={titleChangeHandler} value={Title} />
}

<select><option></option></select>์˜ ๊ฒฝ์šฐ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ <option>์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ 

์ƒ๋‹จ์— Continents๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค. (UploadProductPage ๋ฐ–์—)

const Continents = [
  { key: 1, value: "Africa" },
  { key: 2, value: "Europe" },
  { key: 3, value: "Asia" },
  { key: 4, value: "North America" },
  { key: 5, value: "South America" },
  { key: 6, value: "Australia" },
  { key: 7, value: "Antarctica" }
]

๊ทธ๋ฆฌ๊ณ  map()์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.

<select>
  {Continents.map(item => (
    <option key={item.key} value={item.key}>{item.value}</option>
  ))}
</select>

์ด๊ฒƒ๋„ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— onChange๋ฅผ ์ •์˜ํ•œ๋‹ค.

<select onChange={continentChangeHandler} value={Continent}>
  {Continents.map(item => (
    <option key={item.key} value={item.key}>{item.value}</option>
  ))}
</select>
profile
Frontend Developer

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