230509 - useState, sass/scss(mixin, content, import, extand)

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

๐Ÿšฉ React

useState

๐Ÿ“ ์„ค๋ช…

  • useState ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฐ’์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ (๋™์ )


ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ’์ด 1์”ฉ ์˜ฌ๋ผ๊ฐ€๋Š” ์ปดํฌ๋„ŒํŠธ

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

์ž…๋ ฅ

App.js

import Input from "./Input";
import Input2 from "./Input2";
import "./styles.css";

export default function App() {
  return (
    <div className="App">
      <Input />
      <br />
      <br />
      <Input2 />
    </div>
  );
}

Input.jsx

import React, { useState } from "react";

function Input() {
  const [text, setText] = useState();
  // input๊ฐ’์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ƒํƒœ ์„ค์ •

  const cc = (event) => {
    // console.log(event.target.value);
    setText(event.target.value);
    // input์— ์ž…๋ ฅํ•œ ๋‚ด์šฉ(onChange ์ด๋ฒคํŠธ๋กœ ์ธ์‹ํ•œ ๋‚ด์šฉ)์„ text ๋ณ€์ˆ˜๋กœ ์ง‘์–ด๋„ฃ์Œ
  };
  const reset = () => {
    setText("");
  };

  return (
    <>
      <input type="text" value={text} onChange={cc} />
      <button
        onClick={() => {
          // setText(""); // ์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ input ์ดˆ๊ธฐํ™”
          reset();
        }}
      >
        ์ดˆ๊ธฐํ™”
      </button>
      <div>
        <b>์ž…๋ ฅ๊ฐ’ : </b>
        {text}
      </div>
    </>
  );
}

export default Input;

Input2.jsx

import React, { useState } from "react";

function Input2() {
  const [inputs, setInputs] = useState({
    // ์˜ค๋ธŒ์ ํŠธ ํ˜•์‹
    id: "",
    nickname: ""
  }); // input์˜ ๊ฐ’์ด 2๊ฐœ -> ์˜ค๋ธŒ์ ํŠธ๋กœ

  const { id, nickname } = inputs; // ๋น„๊ตฌ์กฐํ• ๋‹น์„ ํ†ตํ•ด ์ถ”์ถœ

  const change = (event) => {
    console.log("name? : ", event.target.name);
    console.log("value? : ", event.target.value);

    // const { name, value } = event.target; // ์ถ”์ถœ
    const name = event.target.name; // ๋‚ด๊ฐ€ ์ง€์ •ํ•œ ๊ฒƒ
    const value = event.target.value; // ์ž…๋ ฅ๊ฐ’(์›๋ž˜ ์žˆ๋Š” ๊ฒƒ)
    // ๋™์ผํ•œ ์ฝ”๋“œ

    const nextInputs = {
      ...inputs,
      [name]: value
      // ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์ „๋ถ€ ๊ฐ€์ ธ์˜ด(์Šคํ”„๋ ˆ๋“œ๋ฌธ๋ฒ•)
      // [name]์— id, nickname์ด ๋“ค์–ด์™€์„œ ์•ž์„ ๋ฎ์–ด ์”Œ์›€
    };
    setInputs(nextInputs);
  };

  const reset = () => {
    setInputs({
      id: "",
      nickname: ""
    });
  };

  return (
    <>
      <input
        type="text"
        name="id"
        onChange={change}
        placeholder="์ด๋ฆ„"
        value={id}
      />
      <input
        type="text"
        name="nickname"
        onChange={change}
        placeholder="๋‹‰๋„ค์ž„"
        value={nickname}
      />
      <button onClick={reset}>์ดˆ๊ธฐํ™”</button>
      <div>
        <b>์ž…๋ ฅ๊ฐ’ : </b>
        {id}({nickname})
      </div>
    </>
  );
}

export default Input2;

์ถœ๋ ฅ

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


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






๐Ÿšฉ SASS/SCSS

mixin

๐Ÿ“ ์„ค๋ช…

  • ํ•จ์ˆ˜์™€ ๋น„์Šท.
  • ๋ฐ˜๋ณต์ , ์žฌ์‚ฌ์šฉํ•  css ์Šคํƒ€์ผ ๊ทธ๋ฃน์„ ์˜๋ฏธ


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

scss

// mixin ์Šคํƒ€์ผ ์ •์˜
@mixin box-style() {
  width: 100px;
  height: 100px;
  background: orange;
  color: red;
  display: flex;
  justify-content: center;
  align-items: center;

  font: {
    size: 2rem;
    weight: bold;
  }
}

// ์ธ์ž๊ฐ’์„ ์ง€์ •(parameter), ์ธ์ž์— ๊ธฐ๋ณธ๊ฐ’๋„ ์„ค์ •
@mixin box-style2($bg-color: lightgray, $text-color: dimgray) {
  width: 100px;
  height: 100px;
  background: $bg-color;
  color: $text-color;
  display: flex;
  justify-content: center;
  align-items: center;

  font: {
    size: 2rem;
    weight: bold;
  }
}

@mixin border-line($width: 2px, $style: solid, $color: black){
  border: $width $style $color;
}

// ๋ฏน์Šค์ธ ํ˜ธ์ถœ
.b1 {
  @include box-style;
}
.b2 {
  @include box-style;
  color: blue;
}

// ๋ฏน์Šค์ธ ํ˜ธ์ถœ(์ธ์ž๊ฐ€ ์ง€์ •๋œ ์ƒํƒœ)
.b3 {
  // ์ธ์ž๋ฅผ ์ ์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’ ์ ์šฉ
  @include box-style2(powderblue);
  @include border-line;
}
.b4 {
  @include box-style2(#a0c1e2, white);
  @include border-line(10px, dotted, #336699);
}

.wrap {
  display: flex;
  column-gap: 30px;
  row-gap: 30px;
  flex-wrap: wrap;
}

html

<!DOCTYPE html>
<html lang="ko">
  <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>sass05</title>
    <link rel="stylesheet" href="style/main.css" />
  </head>
  <body>
    <div class="wrap">
      <div class="b1">BOX1</div>
      <div class="b2">BOX2</div>
      <div class="b3">BOX3</div>
      <div class="b4">BOX4</div>
    </div>
  </body>
</html>

์ถœ๋ ฅ

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

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






content

๐Ÿ“ ์„ค๋ช…

  • mixin ์‚ฌ์šฉ ์‹œ ์ง€์ •๋œ ์Šคํƒ€์ผ ์ด์™ธ์—๋„ ๋ณ„๋„์˜ ์Šคํƒ€์ผ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

scss

@mixin btn-style() {
  @content;
  width: 100px;
  height: 50px;
  border: none;
}

@mixin btn-style2($hv-bgcolor: gray) {
  @content;
  width: 100px;
  height: 50px;
  border: none;

  // ๋ถ€๋ชจ์— hover
  &:hover {
    background: $hv-bgcolor;
  }
}

.b1 {
  @include btn-style {
    background: coral;
    color: white;
  }
}

.b2 {
  @include btn-style2(skyblue);
  background: beige;
}
.b3 {
  @include btn-style2;
  background: cornflowerblue;
}

html

<!DOCTYPE html>
<html lang="ko">
  <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>sass06</title>
    <link rel="stylesheet" href="style/main.css" />
  </head>
  <body>
    <button class="b1">BUTTON 1</button>
    <button class="b2">BUTTON 2</button>
    <button class="b3">BUTTON 3</button>
    <button class="b4">BUTTON 4</button>
  </body>
</html>

์ถœ๋ ฅ

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

์ผ๋ฐ˜

hover์‹œ


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






import

๐Ÿ“ ์„ค๋ช…

  • Sass์—๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ sass ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ ๋ชจ์œผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์Œ
  • _(์–ธ๋”์Šค์ฝ”์–ด, ์–ธ๋”๋ฐ”)๋กœ ์‹œ์ž‘ํ•˜๋Š” ํŒŒ์ผ์€ compile๋˜์ง€ ์•Š์Œ
    • ํŒŒ์ผ์ด ๋งŽ์•„๋„ css๋Š” ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค์–ด์ง -> ์›น์‚ฌ์ดํŠธ ์„ฑ๋Šฅ๊ฐœ์„ ์— ๋„์›€


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

main.scss

@import "./mixin.scss"; // ๋ถˆ๋Ÿฌ์˜ฌ ๋–ˆ ์–ธ๋”๋ฐ” ์ƒ๋žต
@import "./variables.scss";

div {
  @include box-style($main-color, $sub-color);
  @include border-line;
}

_mixin.scss

@mixin box-style($bg-color: lightgray, $text-color: dimgray) {
  width: 500px;
  height: 500px;
  background: $bg-color;
  color: $text-color;
  display: flex;
  justify-content: center;
  align-items: center;

  font: {
    size: 2rem;
    weight: bold;
  }
}

@mixin border-line($width: 2px, $style: solid, $color: black){
  border: $width $style $color;
}

_variables.scss

$main-color: powderblue;
$sub-color: magenta;

html

<!DOCTYPE html>
<html lang="ko">
  <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>sass07</title>
    <link rel="stylesheet" href="style/main.css" />
  </head>
  <body>
    <div>์—ฌ๋Ÿฌ๊ฐœ์˜ sassํŒŒ์ผ์„ ์‚ฌ์šฉ</div>
  </body>
</html>

์ถœ๋ ฅ

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

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






extand

๐Ÿ“ ์„ค๋ช…

  • ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ์Šคํƒ€์ผ์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™€์„œ ์“ธ ์ˆ˜ ์žˆ๋‹ค


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

scss

/*
  ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ์Šคํƒ€์ผ์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™€์„œ ์“ธ ์ˆ˜ ์žˆ๋‹ค
  @extand ํด๋ž˜์Šค๋ช…
*/

.aaa {
  padding: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: lightblue;
  color: darkblue;
}

.bbb {
  @extend .aaa;
  background: peachpuff;
}

.btn {
  width: 100px;
  height: 40px;
  background: #badbee;
  border: 1px solid #5e84b6;
  border-radius: 8px;
}

input[type="text"] {
  @extend .btn;
  width: 300px;
  padding-left: 20px;
}

/*
  ํ”Œ๋ ˆ์ด์Šคํ™€๋”(placeholder) - ์œ ๋ นํด๋ž˜์Šค %
  @extend์‹œ ๊ฐ€์ ธ๋‹ค ์“ฐ๋Š” ์šฉ๋„๋กœ๋งŒ ๋งŒ๋“ค์–ด์ง
  css ์„ธํŠธ๋“ค์„ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉ
*/

%box {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 2px solid gray;
  width: 200px;
  height: 200px;
}
.box1 {
  @extend %box;
}
.box2 {
  @extend %box;
  background: orange;
}

html

<!DOCTYPE html>
<html lang="ko">
  <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>scss</title>
    <link rel="stylesheet" href="style/main.css" />
  </head>
  <body>
    <h1 class="aaa">Lorem ipsum dolor sit amet</h1>
    <h1 class="bbb">Lorem ipsum dolor sit amet</h1>

    <button class="btn">BUTTON</button>
    <input type="text" placeholder="ํ•˜๊ณ  ์‹ถ์€ ๋ง์„ ๋‚จ๊ฒจ ์ฃผ์„ธ์š”.">

    <br><br>

    <div class="box1">BOX 1</div>
    <br>
    <div class="box2">BOX 2</div>
  </body>
</html>

์ถœ๋ ฅ

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

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






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

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