๊ฐœ๋ฐœ ๐Ÿพ/ReactJS

Toast ํŒ์—… ๋งŒ๋“ค๊ธฐ

JOTOKKI 2021. 7. 6. 10:35
728x90

Toast ํŒ์—…์€ ๋ฌด์–ธ๊ฐ€ ์•ก์…˜์„ ์‹œ์ž‘ํ–ˆ์„ ๋•Œ, ์ข…๋ฃŒ๋˜์—ˆ์„ ๋•Œ, ์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ ๋“ฑ๋“ฑ์˜ ๊ฒฝ์šฐ ์•Œ๋ ค์ฃผ๋Š” ํŒ์—…์ž…๋‹ˆ๋‹ค. 

๋”ฐ๋กœ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ๋‘์ง€ ์•Š๊ณ  ์ผ์ • ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์ž๋™์œผ๋กœ ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค. 

 

JavasScriptํ™˜๊ฒฝ์—์„œ setTimeout์„ ํ†ตํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 

ํ† ์ŠคํŠธ ํŒ์—…

// App.js
import { useState, useEffect } from "react";
import "./styles.css";
export default function App() {
  return (
    <div className="App">
      <div className="btnWrap">
        <button onClick={handleToast}>CLICK</button>
      </div>
    </div>
  );
}
// Toast.js
export default function Toast({ msg = "๋ฉ”์„ธ์ง€ ์—†์Œ" }) {
  return <div className="toast">{msg}</div>;
}
// styles.css
.App {
  font-family: sans-serif;
  text-align: center;
}

.btnWrap {
  padding-top: 50px;
}

button {
  background: lightseagreen;
  color: #fff;
  padding: 10px 15px;
  border-radius: 4px;
  border: 0 none;
  font-weight: bold;
  cursor: pointer;
}

button:hover {
  background: darkcyan;
}

.toast {
  position: absolute;
  top: 50%;
  left: 50%;
  padding: 11px;
  min-width: 200px;
  transform: translate(-50%, -50%);
  z-index: 3;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  border-radius: 4px;
  border: 1px solid #000;
}

Toast์— msg๋ฅผ props๊ฐ’์œผ๋กœ ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•ฉ๋‹ˆ๋‹ค. 

๋งŒ์•ฝ ๋„˜๊ฒจ๋ฐ›๋Š” msg๊ฐ’์ด ์—†๋‹ค๋ฉด "๋ฉ”์„ธ์ง€ ์—†์Œ"์„ ๊ธฐ๋ณธ์œผ๋กœ ์ถœ๋ ฅํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. 

 

Toast๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

1. Toast state๋ฅผ false๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋‘”๋‹ค. 

2. ๋ฒ„ํŠผ ํด๋ฆญ์‹œ Toast state๋ฅผ true๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. 

3. useEffect์—์„œ Toast ๊ฐ’์„ ๋ฆฌ์Šค๋‹ํ•˜๊ณ , Toast๊ฐ€ true์ธ ๊ฒฝ์šฐ 1000msํ›„ ๋‹ค์‹œ false๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. 

 

// App.js
export default function App() {
  const [ToastStatus, setToastStatus] = useState(false);
  const handleToast = () => {
    setToastStatus(true);
  };
  useEffect(() => {
    if (ToastStatus) {
      setTimeout(() => setToastStatus(false), 1000);
    }
  }, [ToastStatus]);
  return (
    <div className="App">
      <div className="btnWrap">
        <button onClick={handleToast}>CLICK</button>
      </div>
      {ToastStatus && <Toast msg="I am Toast ๐Ÿž" />}
    </div>
  );
}

์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. 

์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์จ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ƒํƒœ ๋ฉ”์‹œ์ง€๋ฅผ ๊ฐ์ฒด๋กœ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด์„œ state๋กœ ๊ฐ™์ด ๊ด€๋ฆฌํ•ด ์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 

import { useState, useEffect } from "react";
import "./styles.css";
import Toast from "./Toast";

// ๋ฉ”์„ธ์ง€๋ชฉ๋ก
const msgList = {
  complete: "ํ™•์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.",
  amend: "์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.",
  cancel: "์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
};

export default function App() {
  const [ToastStatus, setToastStatus] = useState(false);
  const [ToastMsg, setToastMsg] = useState(""); // ํ† ์ŠคํŠธ์— ํ‘œ์‹œํ•  ๋ฉ”์„ธ์ง€

  const handleToast = (type) => {
    setToastStatus(true);
    setToastMsg(msgList[type]);
  };

  useEffect(() => {
    if (ToastStatus) {
      setTimeout(() => {
        setToastStatus(false);
        setToastMsg(""); 
      }, 1000);
    }
  }, [ToastStatus]);

  return (
    <div className="App">
      <div className="btnWrap">
        <button onClick={() => handleToast("complete")}>ํ™•์ธ</button>
        <button onClick={() => handleToast("amend")}>์ˆ˜์ •</button>
        <button onClick={() => handleToast("cancel")}>์ทจ์†Œ</button>
      </div>
      {ToastStatus && (
        <>
          <Toast msg={ToastMsg} />
        </>
      )}
    </div>
  );
}

์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌํ•ด ์ฃผ๋ฉด ํ•˜๋‚˜์˜ ํ† ์ŠคํŠธํŒ์—…์œผ๋กœ ๋‹ค์–‘ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋๋‚œ๊ฒƒ ๊ฐ™์ง€๋งŒ ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

๋ฒ„ํŠผ์„ 1000ms ์ด๋‚ด์— ํด๋ฆญํ•  ๋•Œ ๋ฌธ๊ตฌ๋งŒ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

 

๋ฌธ์ œ์ 

์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ† ์ŠคํŠธ ํŒ์—…์˜ ์•ก์…˜์ด ๋๋‚˜๋ฉด ๋™์ž‘์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

export default function App() {
 {...}
  const handleToast = (type) => {
    if (!ToastStatus) { //  ToastStatus๊ฐ€ false์ผ๋•Œ๋งŒ!
      setToastStatus(true);
      setToastMsg(msgList[type]);
    }
  };
  {...}
  return (
    ...
  );
}

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์กฐ๊ฑด๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

๊ทธ๋Ÿผ ToastStatus๊ฐ€ true์—์„œ false๋กœ ๋ฐ”๋€Œ๊ธฐ ์ „๊นŒ์ง€๋Š” ์ƒˆ๋กœ์šด ์ƒํƒœ๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 

 

 

๋ฐ˜์‘ํ˜•