๐Ÿ“† 22.11.06 - CRA with Typescript + Tailwindcss

๋ฒ„๋“คยท2022๋…„ 11์›” 6์ผ
0

โœจToday I Learn (TIL)

๋ชฉ๋ก ๋ณด๊ธฐ
14/62
post-custom-banner

์™œ ์˜ค๋Š˜์€ ๋‚ด์šฉ์ด ์ด๋Ÿด๊นŒ์š”?

์ €๋ฒˆ Kroom (Kream) ํ”„๋กœ์ ํŠธ ๋•Œ ์š”ํ•œ๋‹˜์ด ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์™€ Tailwind๋กœ Carousel๋ฅผ ๋งŒ๋“œ์‹œ๋Š” ๋ชจ์Šต์„ ๋ณด๊ณ  ๊ฐ๋ช…์„ ๋ฐ›์•„
๋ˆ์งˆ๊ธฐ๊ฒŒ ๋”ฐ๋ผ๋ถ™์–ด ๋น„๋ฒ•์„ ๋ฐฐ์›Œ์™”์—ˆ๋‹ค..! ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์™€ Tailwind์— ๋น ๋ฅด๊ฒŒ ์ต์ˆ™ํ•ด์ง๊ณผ ๋™์‹œ์— ํ˜„์žฌ ์ง„ํ–‰์ค‘์ธ ํฐ ๊ทœ๋ชจ์˜ ํ”„๋กœ์ ํŠธ์— ๋„์›€์ด ๋˜๋ ค๋ฉด, ์ง์ ‘ ํ•ด๋‹น ์„œ๋น„์Šค์— ์‚ฌ์šฉ๋˜๋Š” Components ๋ฅผ ๊ธฐํš์ด ๋๋‚˜๊ธฐ ์ „์— ๋งŒ๋“ค์–ด๋ณด๋ฉด์„œ ์—ฐ์Šต์„ ํ•˜๋ฉด ๋‚ด ๋”ด์—์„œ๋Š” ์‹ค๋ ฅ์ด ๊ธˆ๋ฐฉ ๋Š˜ ๊ฒƒ ๊ฐ™์•„ ์ง„ํ–‰ํ•ด ๋ณด๋ ค๊ณ  ํ•œ๋‹ค!


1. CRA with Typescript

์ง€๊ฒน๋„๋ก ํ•ด๋ฒ„๋ฆฐ CRA ์— ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

๋ฐ”๋กœ npx create-react-app [ํ”„๋กœ์ ํŠธ ์ด๋ฆ„] --template typescript ๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅ์„ ํ•˜๋ฉด ์ถ”ํ›„์— ๋”ฐ๋กœ npm install typescript~ ๋“ฑ์˜ ๋ช…๋ น์–ด๋ฅผ ์ณ์„œ ์„ค์น˜ํ•˜์ง€ ์•Š์•„๋„, react์— ํ˜ธํ™˜๋˜๋Š” typescript ๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ฐ™์ด ๊น”๋ ค ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„! ๋‹น์—ฐํžˆ ์ฒ˜์Œ ๋ณด์ด๋Š” ์›์ž๋กœ ๋ชจ์–‘ ๋Œ์•„๊ฐ€๋Š” App.jsx๋Š” ๋‹น์—ฐํžˆ App.tsx๋กœ ๋˜์–ด์žˆ๋‹ค.

2. Tailwind๋ฅผ ์ ์šฉํ•ด๋ณด์ž

์•ž์„œ ๋งํ–ˆ๋“ฏ์ด ๋‚˜๋Š” ๋จผ์ € ์ด๋ก ์„ ์‚ด์ง ์ฐ์–ด ๋จน์–ด๋ณด๊ณ , ๋จธ๋ฆฌ๋ถ€ํ„ฐ ๋“ค์ด๋Œ€๋ฉฐ ๋‹ค์ณ๊ฐ€๋ฉฐ ๋จธ๋ฆฌ์— ๋„ฃ๋Š” ์Šคํƒ€์ผ์ด๋ผ ๋ฐ”๋กœ tailwind๋ฅผ ์„ค์น˜ํ•˜๊ณ , typescript์™€ ํ•จ๊ป˜ ๋งŒ๋“ค์–ด ๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด ์šฐ์„  tailwindcss ๋ฅผ ์„ค์น˜ํ•ด์•ผ๊ฒ ์ง€..

๋ฐฉ๋ฒ•์€ ์—ฌ๊ธฐ ๊ณต์‹ํ™ˆํŽ˜์ด์ง€์— ์žˆ๋‹ค. https://tailwindcss.com/docs/installation
์œ„์˜ ๋งํฌ์—์„œ ํ…Œ์ผ์œˆ๋“œ๋งŒ์˜ ํƒœ๊ทธ๋“ค์— ์ต์ˆ™ํ•ด์ง€๋Š”๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ์ง€๋งŒ, ์ต์ˆ™ํ•ด์ง€๊ธฐ๋งŒ ํ•œ๋‹ค๋ฉด ๊ทธ ์–ด๋– ํ•œ css ํ”„๋ ˆ์ž„์›Œํฌ๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ์›ํ•˜๋Š” UI๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

tailwind ์„ค์น˜ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (์ ์šฉ๋„ ์žˆ์–ด์š”~)

  1. ์•„๋ž˜์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋กœ tailwindcss ๋ฐ ๊ทธ์— ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์„ค์น˜ํ•ด ์ค€ ํ›„, tailwindcss์˜ ์„ค์ •ํŒŒ์ผ์ธ tailwind.config.js ๋ฐ postcss.config.js ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค€๋‹ค.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
  1. ๊ทธ ํ›„ ์•ž์„œ ์ƒ์„ฑํ•œ tailwind.config.js์—์„œ ์‚ฌ์šฉํ•  ํŒŒ์ผ๋“ค์ด ์ธ์‹ํ•  ์ˆ˜ ์žˆ๊ฒŒ content ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด์ค€๋‹ค.
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./src/**/*.{js,jsx,ts,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}
  1. ๊ทธ ํ›„ ๊ฐ€์žฅ root์— ์กด์žฌํ•˜๋Š” index.css์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ…Œ์ผ์œˆ๋“œ๋ฅผ ์ž…ํ˜€์ค€๋‹ค.
@tailwind base;
@tailwind components;
@tailwind utilities;
  1. ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ผ์œˆ๋“œ๋ฅผ ์ ์šฉํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ๋œ๋‹ค.
import React from "react";
import logo from "./logo.svg";
import "./App.css";

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p className="text-3xl font-bold underline-offset-1">
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer">
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

Edit src~ ๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” p ํƒœ๊ทธ์— text์˜ ์‚ฌ์ด์ฆˆ (size and height) ๋ฐ bold, underline-offset๊นŒ์ง€ ํ•œ ๋ฒˆ์— ์ž…ํ˜€๋ณด์•˜๋‹ค.

์ •๋ง ์‰ฝ๊ธดํ•˜๋‹ค.. ๋‹จ์ง€ ๋‹จ์ ์€
1. css ํ”„๋ ˆ์ž„์›Œํฌ๋ผ ์ € ์นœ๊ตฌ๊ฐ€ ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์งœ์•ผ๋˜์„œ ์ฒ˜์Œ์— ์ง„์ž…์žฅ๋ฒฝ์ด ์žˆ๋‹ค.
2. className="ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ์˜ ์„ค๋ช…" ์ด ์•„๋‹Œ css ์š”์†Œ๋“ค๋กœ๋งŒ className ์ด ์ด๋ฃจ์›Œ์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋…์„ฑ์ด ๊ต‰.์žฅ.ํžˆ ์•ˆ์ข‹๋‹ค. ์ถ”ํ›„์— ์œ ์ง€๋ณด์ˆ˜์— ๋“ค์–ด๊ฐ€๋ฉด ๋งŽ์ด ํ—ค๋งฌ๊ฒƒ์ด๋‹ค. (๋‚ด๊ฐ€ ๊ทธ๋žฌ์œผ๋‹ˆ.. ใ…Ž)
-> ๊ทธ๋ž˜์„œ ํ˜‘์—…์„ ํ•  ๋•Œ ์•ฝ์†์„ ์ •ํ•˜๋ฉด ์ข€ ๋‚˜์•„์ง„๋‹ค. ๋‚˜๋Š” ์ด๋ฒˆ์— ๊ผญ๊ผญ, ์ปจํ…Œ์ด๋„ˆ ๋ณ„๋กœ ์ฃผ์„์„ ๋‹ฌ์•„์„œ ์–ด๋””์— ์“ฐ์ด๋Š” ์ฝ”๋“œ์ธ์ง€ ๊ตฌ๋ณ„ํ•˜์ž๊ณ  ํŒ€์›๋ถ„๊ณผ ์•ฝ์†์„ ํ•˜์˜€๋‹ค..

ํ›„๊ธฐ๋Š” ๊ผญ ๋‚จ๊ธธ๊ฒŒ์œ ~

profile
ํƒœ์–ด๋‚œ ๊น€์— ๋งŽ์€ ๊ฒฝํ—˜์„ ํ•˜๋ ค๊ณ  ์•„๋“ฑ๋ฐ”๋“ฑ ์• ์“ฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž
post-custom-banner

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