๋ฐ์ํ
์ด ๊ธ์ udemy์ React ์๋ฒฝ ๊ฐ์ด๋ with Redux, Next.js, TypeScript ๊ฐ์ข๋ฅผ ๋ฐํ์ผ๋ก ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.
67. ๋ชจ๋ ์๊ฐ
- Outputting Dynamic Lists Of Content
- Rendering Contend Under Certain Conditions
68. ๋ฐ์ดํฐ์ ๋ ๋๋ง ๋ชฉ๋ก
- JSX ์ฝ๋์์ map ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋์ ์ธ ๊ตฌ๋ฌธ์ ์คํํด๋ณด๋๋ก ํ์.
- https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/map
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/
๋ฐ์ํ