๊ด€๋ฆฌ ๋ฉ”๋‰ด

C-log

[FE]๋ฌธ์ œ ๋žœ๋ค ๊ตฌํ˜„(2) ๋ณธ๋ฌธ

๐Ÿ’ป๊ฐœ๋ฐœ ๋…ธํŠธ/๋‚˜๋งŒ์˜ ์‹œํ—˜์ง€

[FE]๋ฌธ์ œ ๋žœ๋ค ๊ตฌํ˜„(2)

4:Bee 2024. 2. 27. 22:11
728x90

์ง€๋‚œ๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ์ง€์ ํ–ˆ๋˜ ๋ฌธ์ œ ์ œํ•œ ๊ด€๋ จํ•ด ๋ณด์•ˆํ•œ ๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋“ค์„ ๋ฆฌ๋ทฐํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋จผ์ € ๋ฌธ์ œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” useEffect๋ถ€๋ถ„์„ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.(branch/7)

 
   useEffect(() => {
    fetch('http://localhost:3001/sample')
      .then(res => res.json())
      .then((data) => {
        // ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„, ์ œํ•œ๋œ ๊ฐœ์ˆ˜๋งŒํผ ์ž๋ฅด๊ณ  ์„ž์Œ
        const limitedData = data.slice(0, limit);
        const shuffledData = shuffleArray(limitedData);
        setData(shuffledData);
      })
      .catch((error) => {
        console.error("Error fetching data:", error);
      });
  }, [limit]);

์ด์ „ ์ฝ”๋“œ์™€ ๋‹ฌ๋ฆฌ data๋ฅผ ๋จผ์ € slice๋กœ ๊ฐ€๊ณต์„ ํ•ด์„œ ์›ํ•˜๋Š” ๋ฌธ์ œ ๊ธธ์ด๋งŒํผ ์ œํ•œ์„ ๋‘๊ณ  limitedData ๋ณ€์ˆ˜์— ๋‹ด์•„๊ณ  ํ•ด๋‹น ๋ฐฐ์—ด์„ ๋‹ด์•„๋‘๊ณ  shuffleArray ํ•จ์ˆ˜์— limitedData๋ฅผ ๋„ฃ์–ด์„œ ์šฐ๋ฆฌ๊ฐ€ ์ง€์ •ํ•œ ๋ฌธ์ œ๋“ค๋งŒ ๋ฌธ์ œ๋ฅผ ์„ž์–ด๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ „์ฒด์ฝ”๋“œ๋Š” ์•„๋ž˜ ๋”๋ณด๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

๋”๋ณด๊ธฐ
import React, { useState, useEffect } from "react";
import { Link } from "react-router-dom";

export default function LabExam() {
  const [data, setData] = useState([]);
  const [limit, setLimit] = useState(5);

  useEffect(() => {
    fetch('http://localhost:3001/sample')
      .then(res => res.json())
      .then((data) => {
        // ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„, ์ œํ•œ๋œ ๊ฐœ์ˆ˜๋งŒํผ ์ž๋ฅด๊ณ  ์„ž์Œ
        const limitedData = data.slice(0, limit);
        const shuffledData = shuffleArray(limitedData);
        setData(shuffledData);
      })
      .catch((error) => {
        console.error("Error fetching data:", error);
      });
  }, [limit]);

  // Fisher-Yates ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ๋žœ๋คํ•˜๊ฒŒ ์„ž๋Š” ํ•จ์ˆ˜
  function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  }

  return (
    <div>
      <h2>Test</h2>
      <select onChange={(e) => { setLimit(parseInt(e.target.value)); }}>
        <option value={5}>5</option>
        <option value={10}>10</option>
        <option value={15}>15</option>
        <option value={20}>20</option>
      </select>
      <Link to="/">back</Link>
      <ol>
        {data.map((item) => (
          <li key={item.id}><p>{item.question}</p>{item.options}</li>
        ))}
      </ol>
    </div>
  );
}

์ด์ œ ๋ฌธ์ œ๋ฅผ ์ œํ•œํ•ด์„œ ๋žœ๋ค์œผ๋กœ ์„ž๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅ ํ•ด์กŒ๋‹ค. ํ•˜์ง€๋งŒ ์•„์ง ๋ฒ„ํŠผ์„ ๊ตฌํ˜„ํ•˜์ง€ ๋ชปํ–‡๋‹ค. ๋จผ์ € ๋ฒ„ํŠผ ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฒ„ํŠผ ํƒœ๊ทธ์—์„œ ๋ฌธ์ œ๊ฐ€ ๋žœ๋ค์œผ๋กœ ์„ž์ด๋Š” ํ•ธ๋“ค๋ง ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์ด์ œ suffleArrayํ•จ์ˆ˜๋ฅผ useEffect์—์„œ ๋ฒ—์–ด๋‚  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. suffleArrayํ•จ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•œ useEffect์™€ ํ•ธ๋“ค๋ง ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 
  useEffect(() => {
    fetch('http://localhost:3001/sample')
      .then(res => res.json())
      .then((data) => {
        // ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„, ์ œํ•œ๋œ ๊ฐœ์ˆ˜๋งŒํผ ์ž๋ฅธ๋‹ค.
        setData(data.slice(0, limit));
      })
      .catch((error) => {
        console.error("Error fetching data:", error);
      });
  }, [limit]);
 
// ๋ฌธ์ œ๋ฅผ ์„ž๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฒ„ํŠผ ํด๋ฆญ ํ•ธ๋“ค๋Ÿฌ
  const handleShuffle = () => {
    const shuffledData = shuffleArray(data);
    setData([...shuffledData]); // ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ์„ค์ •ํ•˜์—ฌ React๊ฐ€ ์—…๋ฐ์ดํŠธ๋ฅผ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ
  };
 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ ์šฐ๋ฆฌ๋Š” useEffect์— ์žˆ๋Š” shuffleArrayํ•จ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์›ํ•˜๋Š” ๋ฌธ์ œ ๊ฐœ์ˆ˜๋งŒ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. ์ „์ฒด ์ฝ”๋“œ๋Š” ์•„๋ž˜ ๋”๋ณด๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜๋ผ.

๋”๋ณด๊ธฐ
import React, { useState, useEffect } from "react";
import { Link } from "react-router-dom";

export default function LabExam() {
  const [data, setData] = useState([]);
  const [limit, setLimit] = useState(5);

  useEffect(() => {
    fetch('http://localhost:3001/sample')
      .then(res => res.json())
      .then((data) => {
        // ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„, ์ œํ•œ๋œ ๊ฐœ์ˆ˜๋งŒํผ ์ž๋ฅด๊ณ  ์„ž์Œ
        // const limitedData = data.slice(0, limit);
        // const shuffledData = shuffleArray(limitedData);
        // setData(shuffledData);
        // ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„, ์ œํ•œ๋œ ๊ฐœ์ˆ˜๋งŒํผ ์ž๋ฅธ๋‹ค.
        setData(data.slice(0, limit));
      })
      .catch((error) => {
        console.error("Error fetching data:", error);
      });
  }, [limit]);

  // Fisher-Yates ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ๋žœ๋คํ•˜๊ฒŒ ์„ž๋Š” ํ•จ์ˆ˜
  function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  }

  // ๋ฌธ์ œ๋ฅผ ์„ž๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฒ„ํŠผ ํด๋ฆญ ํ•ธ๋“ค๋Ÿฌ
  const handleShuffle = () => {
    const shuffledData = shuffleArray(data);
    setData([...shuffledData]); // ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ์„ค์ •ํ•˜์—ฌ React๊ฐ€ ์—…๋ฐ์ดํŠธ๋ฅผ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ
  };

  return (
    <div>
      <h2>Test</h2>
      <button onClick={handleShuffle}>Shuffle</button> {/* ๋ฌธ์ œ ์„ž๊ธฐ ๋ฒ„ํŠผ */}
      <select onChange={(e) => { setLimit(parseInt(e.target.value)); }}>
        <option value={5}>5</option>
        <option value={10}>10</option>
        <option value={15}>15</option>
        <option value={20}>20</option>
      </select>
      <Link to="/">back</Link>
      <ol>
        {data.map((item) => (
          <li key={item.id}><p>{item.question}</p>{item.options}</li>
        ))}
      </ol>
    </div>
  );
}

์ด๋ ‡๊ฒŒ ์ œํ•œ๋œ ๋ฌธ์ œ์•ˆ์—์„œ ๋ฌธ์ œ๋ฅผ ์„ž์–ด๋‚ด๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

728x90
Comments