[React] ๐Ÿ“š ๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ค์ •

pyeonneยท2022๋…„ 6์›” 13์ผ
0
post-thumbnail

๐Ÿ“‹ ๊ธฐ์–ตํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ

๐Ÿ“Œ Javascript ํ”„๋กœ์ ํŠธ์— React ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•

  • React๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ์ ์ง„์ ์œผ๋กœ ๋„์ž…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์„ค๊ณ„๋˜์—ˆ๊ณ , ํ•„์š”ํ•œ ๋งŒํผ๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
  • ๋นŒ๋“œ ๋„๊ตฌ ์—†์ด ๋ช‡ ์ค„์˜ ์ฝ”๋“œ๋งŒ์œผ๋กœ React๋ฅผ ๋„์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.
  1. HTML ํŒŒ์ผ์— DOM ์ปจํ…Œ์ด๋„ˆ ์„ค์น˜ํ•˜๊ธฐ
<div id="like_button_container"></div>
  1. React์™€ React DOM์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” script ํƒœ๊ทธ ์ถ”๊ฐ€ํ•˜๊ธฐ
<script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
  1. React ์ปดํฌ๋„ŒํŠธ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด script ํƒœ๊ทธ๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
<script src="like_button.js"></script>

React.createElement()

  • document.createElement(tagName[, options]) ํ•จ์ˆ˜๋Š” ์ง€์ •ํ•œ tagName์˜ HTML ์š”์†Œ๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•˜๊ณ , React.createElement(component, props, ...children) ํ•จ์ˆ˜๋Š” ReactElement๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ์ธ์ž์ธ component์—๋Š” ๋ฌธ์ž์—ด ๋˜๋Š” ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
    • ๋ฌธ์ž์—ด์ผ ๋•Œ๋Š” HTML ํƒœ๊ทธ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ props์—๋Š” compoenent๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์„ ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
    • ์˜ˆ) style, calssName ๋“ฑ
  • ์„ธ ๋ฒˆ์งธ ์ธ์ž์—๋Š” compoenent๊ฐ€ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๋‚ด๋ถ€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
    • ํ…์ŠคํŠธ
    • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ
      - React.createElement() ํ•จ์ˆ˜๋กœ ๋งŒ๋“  ์ปดํฌ๋„ŒํŠธ

ReactDOM.render()

  • ReactElement๋ฅผ ๋ฃจํŠธ์˜ DOM ๋…ธ๋“œ์— ๋ Œ๋”๋ง ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
  • React DOM์€ ReactElement์™€ ์ผ์น˜ํ•˜๋„๋ก DOM์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

๐Ÿ“Œ CRA

  • Node 14.0.0 ํ˜น์€ ์ƒ์œ„ ๋ฒ„์ „ ๋ฐ npm 5.6 ํ˜น์€ ์ƒ์œ„ ๋ฒ„์ „์ด ํ•„์š”ํ•˜๋‹ค.
npx create-react-app my-app
cd my-app
npm start

๐Ÿ“Œ CRA ์—†์ด React ์•ฑ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

Bable์ด JSX๋ฅผ Javascript๋กœ ํŠธ๋žœ์ŠคํŒŒ์ผ๋ง ํ•ด์ฃผ๋Š” ๊ฒƒ์„ ์ง์ ‘ ํ™•์ธํ•ด๋ณด์ž !

react, react-dom ์„ค์ •

npm init --y
npm i react react-dom

public/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

src/index.js

import React from "react";
import ReactDOM from "react-dom";

const domContainer = document.querySelector("#root");

Babel ์„ค์ •

npm i -D @babel/core @babel/cli @babel/preset-react @bable/preset-env
  • @babel/preset-react ํŒจํ‚ค์ง€๋Š” React์™€ ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผ ํ•ด์ค€๋‹ค.
  • @babel/cli ํŒจํ‚ค์ง€๋Š” ํ„ฐ๋ฏธ๋„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด Babel์„ ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.
  • @babel/core ํŒจํ‚ค์ง€๋Š” Babel์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•„์ˆ˜์ ์œผ๋กœ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค.
  • -D ์˜ต์…˜์€ devDependencies์— ์„ค์น˜ํ•œ๋‹ค.
  • preset์€ ๊ต‰์žฅํžˆ ๋‹ค์–‘ํ•œ๋ฐ, ๋งค๋ฒˆ ์ ์šฉํ•  preset์„ ๋ชจ๋‘ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ๋ฒˆ๊ฑฐ๋กญ๋‹ค. ๊ทธ๋ž˜์„œ babel.config.js๋ผ๋Š” ๋ณ„๋„์˜ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด Babel์— ๊ด€๋ จ๋œ ์„ค์ •์„ ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค.

babel.confing.js

module.exports = function (api) {
  api.cache(true);

  const presets = ["@babel/preset-env", "@babel/preset-react"];
  const plugins = [];

  return {
    presets,
    plugins,
  };
};
npx babel {ํŒŒ์ผ๋ช…}

Webpack ์„ค์ •

  • Webpack์€ ๋ฒˆ๋“ค๋Ÿฌ(Bundler)๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ, ๋ฒˆ๋“ค๋Ÿฌ๋Š” ๋ถ„๋ฆฌ๋œ Javascript์™€ CSS ๋ชจ๋“ˆ ์ฝ”๋“œ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์— ์ตœ์ ํ™”๋œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ๋กœ ๊ฒฐํ•ฉํ•œ๋‹ค.
npm i -D webpack webpack-cli html-webpack-plugin babel-loader
  • html-webpack-plugin์€ bundle.js๋ฅผ index.html์— ์ ์šฉํ•ด ์ตœ์ข…์ ์ธ html ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  mode: "development",
  entry: "./src/index.js",
  module: {
    rules: [{ test: /\.js$/, use: "babel-loader" }],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./public/index.html",
    }),
  ],
  optimization: { minimizer: [] },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js",
  },
};

HMR ์„ค์ •

  • Hot Module Replacement(HMR)์€ ๋ชจ๋“ˆ ์ „์ฒด๋ฅผ ๋‹ค์‹œ ๋กœ๋“œํ•˜์ง€ ์•Š๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ๋ณ€๊ฒฝํ•ด๋„ ์ฆ‰์‹œ ์—…๋ฐ์ดํŠธ๋œ๋‹ค.
npm i -D  webpack-dev-server

webpack.config.js

...
  devServer: {
    static: "./dist",
    hot: true,
  },
};
npx webpack serve

๐ŸŒ ์ˆ˜๊ฐ•ํ•œ ์†Œ๊ฐ & ๋– ์˜ค๋ฅด๋Š” ์ƒ๊ฐ

  • babel๊ณผ webpack์„ ์„ค์ •ํ•  ๋•Œ ์™œ ์ด๋Ÿฐ ์˜ต์…˜๋“ค์„ ์ฃผ๋Š”์ง€ ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ, ์ด๋ฒˆ ๊ฐ•์˜๋ฅผ ํ†ตํ•ด์„œ ํ™•์‹คํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ณ  ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๋‚ฏ์„ค์–ด์„œ ์–ด๋ ต๊ฒŒ ๋‹ค๊ฐ€์™”์„ ๋ฟ ์ดํ•ดํ•˜๊ณ  ๋‚˜๋‹ˆ ๊ทธ๋ ‡๊ฒŒ ์–ด๋ ค์šด ๋‚ด์šฉ์€ ์•„๋‹ˆ์—ˆ๋‹ค.

๐Ÿ”— ์ฐธ๊ณ  ๋ž˜ํผ๋Ÿฐ์Šค

profile
๊ฑด๊ฐ•ํ•œ ๋ชธ๊ณผ ๋งˆ์Œ์—์„œ ๊ฑด๊ฐ•ํ•œ ์ฝ”๋“œ๊ฐ€ ํƒœ์–ด๋‚œ๋‹ค !

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