2021. 11. 15. 00:16ใ๐ React
1. ๋ฒํผ ํด๋ฆญ ์ useState๋ฅผ ํ์ฉํด type์ ๋ณ์ ๊ฐ์ ๋ณ๊ฒฝํด์ฃผ๊ณ ์ถ์ ๋
์ฝ๋์ ์ค๋ณต๋๋ ๋ถ๋ถ์ ํจ์๋ก ์์ฑํด ์ ๋ฌํด ์ฃผ๋ คํ๋๋ฐ ๋ฌดํ ๋ฐ๋ณต์ด ๋์๊ฐ๋ค.
const [type, setType] = useState('');
const handleSortChange = (type) => {
setType(type);
};
์ด๋ ๋ฒํผ์ ํด๋ฆญํ์ ๋ ํจ์๋ฅผ ์ ๋ฌํ๋ฉด ๋ฐ์ํ๋ ์๋ฌ๋ก
<button onClick={ handleSortChange("date") }>button</button>
์ ํจ์๊ฐ ์คํ๋ ๋ state๋ฅผ ๋ณํํ๋ ์ฝ๋์ด๊ธฐ ๋๋ฌธ์ ๊ณ์ ๋ ๋๋๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
state ๋ณํ์ํฌ ๋ ์๋ ๋ฐฉ๋ฒ์ผ๋ก ์์ฑ
- ( ) => arrow function์ผ๋ก ํจ์๋ฅผ ์ ๋ฌํด์ฃผ๊ฑฐ๋
- ( ) => ๋ฐ๋ก setType() ์ฝ๋๋ฅผ ์์ฑํด ๋ณ์๋ฅผ ์ ๋ฐ์ดํธ ํ๊ฑฐ๋
- ๋ ์์์ id ๊ฐ์ ์ง์ ํด์ฃผ๊ณ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ์ ๋ฌํด e.target.getAttribute("id") ํน์ e.target.id์ผ๋ก ๋ณ์๊ฐ ์ ๋ฐ์ดํธ
<button onClick={()=>handleSortChange("date")}>button</button>
<button onClick={()=>setType("date")}>button</button>
<button onClick={(e)=>handleSortChange(e)}>button</button>
- ์ ์ฒด ์ฝ๋ ํ์ธ
import { useState } from "react";
export default function App() {
const [type, setType] = useState(null);
const handleType = (type) => {
setType(type);
};
const handleTypeWithId = (e) => {
setType(e.target.getAttribute("id")); // e.target.id
};
return (
<div className="App">
<button onClick={() => handleType("btn1")}>btn 1</button> // 1๋ฒ. ์ง์ type ๋ณ์๊ฐ ์ ๋ฌ
<button onClick={() => setType("btn2")}>btn2</button> // 2๋ฒ. ํจ์ ์ ๋ฌ์์ด ๋ฐ๋ก setType ์ฝ๋ ์์ฑ
<button onClick={(e) => handleClick(e)}>btn2</button> // 3๋ฒ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ์ด์ฉํด id๊ฐ ๋ฐ์์ type ๋ณ์๊ฐ ๋ณ๊ฒฝ
</div>
);
}
2. className์ ์ ์ฉํ๋ ๋ค์ํ ๋ฐฉ๋ฒ
module.css๋ฅผ ์์ฃผ ์ฐ๋ค ๋ณด๋ ๊ธฐ๋ณธ ํด๋์ค๋ช ์ ์ฉํ๋ ๋ฒ์ด ๊ณง์ฅ ๊ธฐ์ต๋์ง ์์๋ค.
์ด๋ฒ ์ ๋ฆฌ๋ฅผ ํตํด ํ์คํ ๊ธฐ์ตํ ์ ์์๋ค.
- ๊ธฐ๋ณธ html์์๋ DOM ์์๋ค์ class์ css ํด๋์ค๋ช ์ ์์ฑํด์ค๋ค.
<p class="one"> html class </p>
๋ฆฌ์กํธ์์๋ JSX ๋ฌธ๋ฒ์ผ๋ก className์ ์ฌ์ฉํด ์คํ์ผ์ ์ ์ฉํด์ค๋ค.
1 ) module.css
- ๊ธฐ๋ณธ ํด๋์ค๋ช ์์ฑ
<p className={styles.one}> module css 1 </p>
- css 2๊ฐ ์ง์ ์
<p className={`${styles.one} ${styles.two}`}> module css 2 </p>
<p className={[ style.one, style.two ]}.join(' ')}> module css 2 </p>
- className์ '-'๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ ์์ฑ๋ฒ
<p className={styles['one-type']}> module css 3 </p>
2) ๊ธฐ๋ณธ react css
<p className="one"> react css </p>
<p className="one-type two"> react css </p>
- ์ผํญ ์ฐ์ฐ์๋ฅผ ํ์ฉํด ์ํฉ๋ณ css ์ง์
<p className={num == 0 ? "one" : "one two"}> react css 2 </p>
3. table ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ ( ์ผ์๋ณ ์ ๋ ฌ )
const TABLE_DATA = [
{
title: "์ค๋์ ์์
์ด ์์ต๋๋ค.",
viewCount: 12,
date: "2021-11-11"
},
{
title: "๋ด์ผ์ ๋ธ๋ก๊ทธ ํฌ์คํ
์ ์ ๋ฆฌํฉ์๋ค.",
viewCount: 22,
date: "2021-10-10"
},
{
title: "๋ฌธ์ํ๋ฅผ ์์ง ๋ง์๋ค.",
viewCount: 2,
date: "2020-10-10"
}
];
์์ ๊ฐ์ ์ ๋ฌ๋ ๋ฐ์ดํฐ ์ค 'viewCount' ๊ฐ์ ์ซ์ ๋ณ์๋ sort ํจ์๋ฅผ ํตํด ์ ๋ ฌํ ์ ์์ง๋ง,
๋ฌธ์์ด๋ก ์ ๋ฌ๋ ๋ ์ง ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํ๊ธฐ ์ํด์ new Date( ).getTime( )๋ฅผ ํ์ฉํด ์ ๋ฌ๋ ์ผ์๋ฅผ ์ซ์๋ก ๋ณํํจ์ผ๋ก์จ ์์๋ฅผ ์ ๋ ฌํ ์ ์๋ค.
let data = tableData.sort((a,b) => {
if(sortedType === 'viewCount') return b[sortedType] - a[sortedType];
else return new Date(b[sortedType]).getTime() - new Date(a[sortedType]).getTime();
})
์ฌ์ฉ์ ํด๋ฆญ์ ํตํด ์ ๋ ฌ ํ์ ์ด ๋ฐ๋๋ฉด data ๋ณ์๊ฐ ์ฌ์ ๋ ฌ๋๋ค.
sortedType์ ๋ ์ง๊ฐ ์ ๋ฌ๋ ๊ฒฝ์ฐ
๊ฐ ๊ฐ์ฒด์ ์ผ์๊ฐ์ ์ซ์๋ก ๋ณํํด ์ ๋ ฌ๋ ๊ฐ์ ๋ฐํํด์ค๋ค. ( ex. getTime()์ ํตํด )
function Table({ tableData }) {
const [sortedType, setSortedType] = useState('date');
let data = [...tableData].sort((a, b) => { // type์ ๋ฐ๋ฅธ ๋ฐ์ดํฐ ์ ๋ ฌ
if (sortedType == "viewCount") return a[sortedType] - b[sortedType];
if (sortedType == "date"){
return new Date(b[sortedType]).getTime() - new Date(a[sortedType]).getTime();
}
});
return (
<div>
<table>
<thead>
<tr>
<th>Title</th>
<th onClick={()=>setSortedType("viewCount")}>ViewCount</th> // ํด๋ฆญ์ ์ ๋ ฌ ํ์
์
๋ฐ์ดํธ
<th onClick={()=>setSortedType("date")}>Date</th>
</tr>
</thead>
<tbody>
{data.map((data, index) => {
return (
<tr key={index}>
<td>{data.title}</td>
<td>{data.ViewCount}</td>
<td>{data.date}</td>
</tr>
);
})}
</tbody>
</table>
</div>
);
}
export default Articles;
Posted by Ang