230501 - React ๊ธฐ์ดˆ

๋ฐฑ์Šน์—ฐยท2023๋…„ 5์›” 1์ผ
1

๐Ÿšฉ React ๊ธฐ์ดˆ

codesandbox

๐Ÿ“ ์„ค๋ช…

  • ์˜จ๋ผ์ธ ์ฝ”๋“œ ์—๋””ํ„ฐ
  • ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๊ตฌ์ถ• ์—†์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์›น์œผ๋กœ ์ฝ”๋”ฉํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋ฆฌ์•กํŠธ ๊ฐœ๋ฐœ ํ™”๋ฉด


JSX

๐Ÿ“ ์„ค๋ช…

html + javascript -> jsx๋ฐฉ์‹(javascript syntax extension)

  1. ์ˆ˜์ •์„ ํ•˜๋ฉด ์ƒˆ๋กœ ๋ฆฌ๋กœ๋“œ ํ•˜์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ ์ ์šฉ
  2. class๊ฐ€ ์•„๋‹ˆ๋ผ className
  3. ํ•˜๋‚˜์˜ ์š”์†Œ๋งŒ ๋ฆฌํ„ด๋จ / ํ˜•์ œ ๋…ธ๋“œ๋ฅผ ์“ธ ์ˆ˜ ์—†๊ณ  ๋ฌถ์–ด์ค˜์•ผ ํ•จ
  • <></>
  • <Fragment><Fragment> : import React from "react"๊ฐ€ ํ•„์š”ํ•จ
  • <React.Fragment></React.Fragment> : import React from "react"๊ฐ€ ํ•„์š”ํ•จ
  1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ(๋ณ€์ˆ˜, ํ•จ์ˆ˜ ...) ์‚ฌ์šฉ ์‹œ { } ์ค‘๊ด„ํ˜ธ
    ex) const name = "ใ…Žใ…‡"
    { name }'s Diary
    { ${name}์˜ ์˜ค๋Š˜ ์•„์นจ ๋ฉ”๋‰ด }

    4-1. ์˜ค๋ธŒ์ ํŠธ๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ์ค‘๊ด„ํ˜ธ ์‚ฌ์šฉ
    4-2. ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์Šคํƒ€์ผ์‹œํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ •์˜ (return ๋‚ด๋ถ€)
    ex) style={{width: "200px"}};

  2. ํƒœ๊ทธ๋Š” ๊ผญ ์Œ์œผ๋กœ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋‹ซ๋Š” ํƒœ๊ทธ ์—†์ด ํ™€๋กœ ์กด์žฌํ•˜๋Š” ํƒœ๊ทธ๋“ค ๋’ค์—๋Š” ๋ฌด์กฐ๊ฑด '/'์„ ๋ถ™์—ฌ์•ผ ํ•จ (์…€ํ”„ํด๋กœ์ง•)

  • <br />
  • <img src="" alt="" />

์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜(ํ‘œ์‹œ)ํ•  ๋–„๋Š” ํ•จ์ˆ˜๋กœ, ํ•จ์ˆ˜๋ช…์€ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘



โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

  • React
import { Fragment } from "react";
// import React from "react";
import "./styles.css";

export default function App() {
  const name = "๋ˆ„๋ฆฌ";
  const style = { color: "green" };
  const lists = ["์šฐ์œ ", "ํ† ์ŠคํŠธ", "๊ณ„๋ž€"];
  const click = () => {
    alert("๊ตฟ๋ชจ๋‹");
  };

  // ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๋Š” ๋ถ€๋ถ„์ด ๋งŽ์Œ
  return (
    <>
      <div className="nav">
        <h1>{name}'s Diary</h1>
      </div>
      <p style={style}>{`${name}์˜ ์˜ค๋Š˜ ์•„์นจ ๋ฉ”๋‰ด`}</p>
      <ul>
        {/* <li>์šฐ์œ </li>
        <li>ํ† ์ŠคํŠธ</li>
        <li>๊ณ„๋ž€</li> */}

        {lists.map((item) => {
          // map์ด ํ™•์‹คํ•œ ๋ฆฌํ„ด๊ฐ’์„ ๊ฐ€์ง
          return <li>{item}</li>;
          // return console.log(item);
        })}
      </ul>
      <img
        style={{ width: "200px", border: "5px solid green" }}
        src="https://images.unsplash.com/photo-1525351484163-7529414344d8?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=880&q=80"
        alt=""
        title="ํด๋ฆญํ•ด๋ณด์„ธ์š”"
        onClick={click}
      />
    </>
  );
}

  • index.js
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";

import App from "./App";

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <App />
  </StrictMode>
);

  • index.html
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<meta name="theme-color" content="#000000">
	<!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
	<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
	<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
	<!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
	<title>React App</title>
</head>

<body>
	<noscript>
		You need to enable JavaScript to run this app.
	</noscript>
	<div id="root"></div>
	<!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
</body>

</html>

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

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