Framework/React ์™„๋ฒฝ ๊ฐ€์ด๋“œ

[React ์™„๋ฒฝ ๊ฐ€์ด๋“œ] section 5 : ๋ Œ๋”๋ง ๋ฆฌ์ŠคํŠธ ๋ฐ ์กฐ๊ฑด๋ถ€ Content

yuri lee 2022. 9. 18. 16:54
๋ฐ˜์‘ํ˜•
์ด ๊ธ€์€ udemy์˜ React ์™„๋ฒฝ ๊ฐ€์ด๋“œ with Redux, Next.js, TypeScript ๊ฐ•์ขŒ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

67.  ๋ชจ๋“ˆ ์†Œ๊ฐœ

  • Outputting Dynamic Lists Of Content
  • Rendering Contend Under Certain Conditions

68.  ๋ฐ์ดํ„ฐ์™€ ๋ Œ๋”๋ง ๋ชฉ๋ก

return (
    <div>
      <Card className="expenses">
        <ExpensesFilter
          selected={filteredYear}
          onChangeFilter={filterChangeHandler}
        />
        {props.items.map((expense) => (
          <ExpenseItem
            title={expense.title}
            amount={expense.amount}
            date={expense.date}
          />
        ))}
        {/* <ExpenseItem
          title={props.items[0].title}
          amount={props.items[0].amount}
          date={props.items[0].date}
        />
        <ExpenseItem
          title={props.items[1].title}
          amount={props.items[1].amount}
          date={props.items[1].date}
        />
        <ExpenseItem
          title={props.items[2].title}
          amount={props.items[2].amount}
          date={props.items[2].date}
        />
        <ExpenseItem
          title={props.items[3].title}
          amount={props.items[3].amount}
          date={props.items[3].date}
        /> */}
      </Card>
    </div>
  );

69. State ์ €์žฅ ๋ชฉ๋ก ์‚ฌ์šฉ

  • ์ดˆ๊ธฐ expenses data ๋ฅผ state ๋ฅผ ๋„ฃ์–ด ์‚ฌ์šฉํ•ด๋ณด์ž.
  const [expenses, setExpenses] = useState(DUMMY_EXPENSES);

  const addExpenseHandler = (expense) => {
    // setExpenses([expense, ...expenses])
    setExpenses((prevExpenses) => { // more saftey method! 
      return [expense, ...prevExpenses];
    });
  };

70. "keys" ์ดํ•ดํ•˜๊ธฐ

  • ๊ณ ์œ  ์‹๋ณ„์ž๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ๋ฆฌ์•กํŠธ๋Š” ๋ชจ๋“  ์•„์ดํ…œ๋“ค์„ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๊ณ , ๋ฐฐ์—ด์˜ ๊ธธ์ด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ item์ด ์œ„์น˜ํ•ด์•ผ ํ•  ๊ณณ๊นŒ์ง€ ์ธ์‹ํ•œ๋‹ค. ๋” ํšจ์œจ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ๋‹ค. 
  • ๋ชฉ๋ก์˜ item์„ ๋งตํ•‘ํ•  ๋•Œ๋Š” ํ•ญ์ƒ key๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. 

→ key ๊ฐ€ ์—†์„ ๋•Œ๋Š” ์ƒˆ๋กœ์šด item element๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š๊ณ , ๊ธฐ์กด์˜ item ์—์„œ title, amount, date ์˜ ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚ฌ๋‹ค. 

 

71.  ์—ฐ์Šตํ•˜๊ธฐ : List ๋‹ค๋ฃจ๊ธฐ

  • ๋…„๋„์— ๋งž์ถฐ์„œ ํ•„ํ„ฐ๋ง ๋˜๋„๋ก List ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์ž
const Expenses = (props) => {
  const [filteredYear, setFilterYear] = useState("2020");
  // const [filteredExpense, setFilteredExpense] = useState(props.items);

  const filterChangeHandler = (selectedYear) => {
    setFilterYear(selectedYear);
    // const result = props.items.filter(
    //   (expense) => expense.date.getFullYear() == selectedYear
    // );
    // setFilteredExpense(result);
  };

  const filteredExpenses = props.items.filter((expense) => {
    return expense.date.getFullYear().toString() === filteredYear;
  });

  return (
    <div>
      <Card className="expenses">
        <ExpensesFilter
          selected={filteredYear}
          onChangeFilter={filterChangeHandler}
        />
        {filteredExpenses.map((expense) => (
          <ExpenseItem
            key={expense.id}
            title={expense.title}
            amount={expense.amount}
            date={expense.date}
          />
        ))}
      </Card>
    </div>
  );
};

export default Expenses;

→ ์ฃผ์„ ๋ถ€๋ถ„์€ ๋ฏธ๋ฆฌ ์—ฐ์Šตํ•ด๋ดค๋˜ ๊ฒƒ. ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๊ตฐ!..๐Ÿคซ 

 

73. 74. 75. ์กฐ๊ฑด๋ถ€ ๋‚ด์šฉ ์ถœ๋ ฅํ•˜๊ธฐ

  • ํ—ค๋‹น๋˜๋Š” ๋…„๋„ item์ด ์—†์„ ๊ฒฝ์šฐ ์—†๋‹ค๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•ด์ค˜์•ผ ํ•œ๋‹ค. 
  return (
    <div>
      <Card className="expenses">
        <ExpensesFilter
          selected={filteredYear}
          onChangeFilter={filterChangeHandler}
        />
        {filteredExpenses.length === 0 && <p>No expenses found.</p>}
        {filteredExpenses.length > 0 &&
          filteredExpenses.map((expense) => (
            <ExpenseItem
              key={expense.id}
              title={expense.title}
              amount={expense.amount}
              date={expense.date}
            />
          ))}
      </Card>
    </div>
  );
  let expensesContent = <p>No expenses found.</p>;
  if (filteredExpenses.length > 0) {
    expensesContent = filteredExpenses.map((expense) => (
      <ExpenseItem
        key={expense.id}
        title={expense.title}
        amount={expense.amount}
        date={expense.date}
      />
    ));
  }
ใ„ด
  return (
    <div>
      <Card className="expenses">
        <ExpensesFilter
          selected={filteredYear}
          onChangeFilter={filterChangeHandler}
        />
        {expensesContent}
      </Card>
    </div>
  );

์‚ผํ•ญ์—ฐ์‚ฐ์ž, &&์—ฐ์‚ฐ์ž, ๋ณ€์ˆ˜ ์‚ฌ์šฉ๋“ฑ์˜ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•œ๋‹ค. ๊ฐ€๋…์„ฑ์˜ ์ฐจ์ด

77.  ๋ฐ๋ชจ ์•ฑ: ์ฐจํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ 

  • Chart component ์ถ”๊ฐ€
  • ChartBar component ์ถ”๊ฐ€

78. ๋™์  ์Šคํƒ€์ผ ์ถ”๊ฐ€ํ•˜๊ธฐ 

const Chart = (props) => {
  let barFillHeight = "0%";

  if (props.max > 0) {
    barFillHeight = Math.round((props.value / props.maxValue) * 100) + "%";
  }

  return (
    <div className="chart-bar">
      <div className="chart-bar__inner">
        <div
          className="chart-bar__fill"
          style={{ height: barFillHeight }}
        ></div>
        <div className="chart-bar__label">{props.label}</div>
      </div>
    </div>
  );
};

79.  ๋งˆ๋ฌด๋ฆฌ ๋ฐ ๋‹ค์Œ ๋‹จ๊ณ„

  • ExpensesChart component ์ถ”๊ฐ€
  • for in / for of ์ฐพ์•„๋ณด๊ธฐ

80.  ์ž‘์€ ๋ฒ„๊ทธ ์ˆ˜์ •

  • ๊ฐ’ ์ถ”๊ฐ€ ์‹œ amout๊ฐ€ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž์—ด๋กœ ์ถ”๊ฐ€๋˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ + ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•ด์ง€๋„๋ก ์ˆ˜์ •

https://www.udemy.com/course/best-react/

 

๋ฐ˜์‘ํ˜•