티스토리 뷰
728x90
반응형
useState & Record(type script)를 활용한 상태 관리
import { useState } from "react";
function Menu() {
const [toggleSubMenu, setToggleSubMenu] = useState<Record<string, boolean>>({});
const handleToggleSubMenu = (menuName: string) => {
setToggleSubMenu((prevState) => ({
...prevState,
[menuName]: !prevState[menuName],
}));
}
return (
<nav id="menu">
<header className="major">
<h2>Menu</h2>
</header>
<ul>
<li><a href="index.html">Homepage</a></li>
<li><a href="generic.html">Generic</a></li>
<li><a href="elements.html">Elements</a></li>
<li>
<span className={toggleSubMenu['submenu1'] ? 'opener active' : 'opener'} onClick={() => handleToggleSubMenu('submenu1')}>Submenu</span>
<ul>
<li><a href="#">Lorem Dolor</a></li>
<li><a href="#">Ipsum Adipiscing</a></li>
<li><a href="#">Tempus Magna</a></li>
<li><a href="#">Feugiat Veroeros</a></li>
</ul>
</li>
<li><a href="#">Etiam Dolore</a></li>
<li><a href="#">Adipiscing</a></li>
<li>
<span className={toggleSubMenu['submenu2'] ? 'opener active' : 'opener'} onClick={() => handleToggleSubMenu('submenu2')}>Another Submenu</span>
<ul>
<li><a href="#">Lorem Dolor</a></li>
<li><a href="#">Ipsum Adipiscing</a></li>
<li><a href="#">Tempus Magna</a></li>
<li><a href="#">Feugiat Veroeros</a></li>
</ul>
</li>
<li><a href="#">Maximus Erat</a></li>
<li><a href="#">Sapien Mauris</a></li>
<li><a href="#">Amet Lacinia</a></li>
</ul>
</nav>
);
}
export default Menu
useReducer를 활용한 상태 관리
import { useState, useReducer } from "react";
type State = Record<string, boolean>;
type Action = { type: "toggle"; menuName: string };
function reducer(state: State, action: Action): State {
switch (action.type) {
case "toggle":
return {
...state,
[action.menuName]: !state[action.menuName],
};
default:
throw new Error("Unknown action type");
}
}
function Menu() {
const [toggleSubMenu, dispatch] = useReducer(reducer, {});
return (
<nav id="menu">
<header className="major">
<h2>Menu</h2>
</header>
<ul>
<li><a href="index.html">Homepage</a></li>
<li><a href="generic.html">Generic</a></li>
<li><a href="elements.html">Elements</a></li>
<li>
<span className={toggleSubMenu['submenu1'] ? 'opener active' : 'opener'} onClick={() => dispatch({ type: "toggle", menuName: "submenu1" })}>Submenu</span>
<ul>
<li><a href="#">Lorem Dolor</a></li>
<li><a href="#">Ipsum Adipiscing</a></li>
<li><a href="#">Tempus Magna</a></li>
<li><a href="#">Feugiat Veroeros</a></li>
</ul>
</li>
<li><a href="#">Etiam Dolore</a></li>
<li><a href="#">Adipiscing</a></li>
<li>
<span className={toggleSubMenu['submenu2'] ? 'opener active' : 'opener'} onClick={() => dispatch({ type: "toggle", menuName: "submenu2" })}>Another Submenu</span>
<ul>
<li><a href="#">Lorem Dolor</a></li>
<li><a href="#">Ipsum Adipiscing</a></li>
<li><a href="#">Tempus Magna</a></li>
<li><a href="#">Feugiat Veroeros</a></li>
</ul>
</li>
<li><a href="#">Maximus Erat</a></li>
<li><a href="#">Sapien Mauris</a></li>
<li><a href="#">Amet Lacinia</a></li>
</ul>
</nav>
);
}
export default Menu
Sub Component 활용한 상태 관리
import SubMenu from "./SubMenu";
function Menu() {
return (
<nav id="menu">
<header className="major">
<h2>Menu</h2>
</header>
<ul>
<li><a href="index.html">Homepage</a></li>
<li><a href="generic.html">Generic</a></li>
<li><a href="elements.html">Elements</a></li>
<SubMenu title="Submenu">
<li><a href="#">Lorem Dolor</a></li>
<li><a href="#">Ipsum Adipiscing</a></li>
<li><a href="#">Tempus Magna</a></li>
<li><a href="#">Feugiat Veroeros</a></li>
</SubMenu>
<li><a href="#">Etiam Dolore</a></li>
<li><a href="#">Adipiscing</a></li>
<SubMenu title="Another Submenu">
<li><a href="#">Lorem Dolor</a></li>
<li><a href="#">Ipsum Adipiscing</a></li>
<li><a href="#">Tempus Magna</a></li>
<li><a href="#">Feugiat Veroeros</a></li>
</SubMenu>
<li><a href="#">Maximus Erat</a></li>
<li><a href="#">Sapien Mauris</a></li>
<li><a href="#">Amet Lacinia</a></li>
</ul>
</nav>
);
}
export default Menu
import { useState } from "react";
function SubMenu({title, children}: {title:string, children:React.ReactNode}) {
const [isOpen, setIsOpen] = useState<boolean>(false);
const toggleSubMenu = () => {
setIsOpen(!isOpen);
};
return (
<li>
<span className={isOpen ? "opener active" : "opener"} onClick={toggleSubMenu}>
{title}
</span>
<ul>{children}</ul>
</li>
);
}
export default SubMenu
728x90
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- system.io
- 인텔리제이(intellij)
- In App Purchase
- java 키워드 정리
- 스프링 프레임워크(spring framewordk)
- 문자 자르기
- REST API
- .submit()
- java-개발 환경 설정하기
- 스프링 시큐리티(spring security)-http basic 인증
- java.sql
- jstl(java standard tag library)-core
- 진수 변환
- await
- 메이븐(maven)
- 특정 문자를 기준으로 자르기
- 표현 언어(expression language)
- 람다식(lambda expression)
- MainActor
- error-java
- java web-mvc
- jsp 오픈 소스
- React
- 스프링 시큐리티(spring security)
- System.Diagnostics
- 스프링 프레임워크(spring framework)
- jstl(java standard tag library)
- 상품 등록
- nl2br
- 제품 등록
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함