반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- component
- UE5
- state
- MySQL
- BinaryTree
- priority_queue
- leetcode
- nodeJS
- Navigation
- JSX
- DP
- c++
- bit
- css
- Context
- route
- Props
- event
- array
- queue
- 비트연산
- Callback
- axios
- React
- map
- server
- routes
- count
- node.js
- treenode
Archives
- Today
- Total
우사미 코딩
[React] Navigation 만들기 (6) - Custom navigation hook 만들기 본문
반응형
import { useNavigation } from "react-router-dom";
function Route({path, children}){
const {currentPath } = useNavigation();
if(path === currentPath){
return children;
}
return null;
}
export default Route;
custom navigation hook을 만드는 목적 :
다른 엔지니어들이 네비게이션을 쉽게 사용할 수 있도록 함. 약간의 타이핑을 줄여준당
src/hooks/use-navigation.js
import NavigationContext from "../context/navigation";
import { useContext } from "react";
function useNavigation(){
return useContext(NavigationContext);
}
export default useNavigation;
이렇게 커스텀 훅을 만들었따!
그럼 Route.js에서 navigation hook을 사용하도록 수정하자
- src/component/Route.js
import { useContext } from "react";
import NavigationContext from "../context/navigation";
function Route({path, children}){
const {currentPath } = useContext(NavigationContext);
if(path === currentPath){
return children;
}
return null;
}
export default Route;
->
//import { useNavigation } from "../hooks/use-navigation"; // 이러면 에러
import useNavigation from "../hooks/use-navigation";
function Route({path, children}){
const { currentPath } = useNavigation();
if(path === currentPath){
return children;
}
return null;
}
export default Route;
- Link.js에서도 hook을 사용하도록 변경한다
import { useContext } from "react";
import NavigationContext from "../context/navigation";
import classNames from "classnames";
function Link({ to, children }){
const {navigate} = useContext(NavigationContext);
const classes = classNames('text-blue-500');
const handleClick = (event) => {
if(event.metaKey || event.ctrlKey){ // ctrl 키 눌렀을 때 새로운 탭에서 열기
return;
}
event.preventDefault();
navigate(to);
}
return <a className={classes} href={to} onClick={handleClick}>{children}</a>
}
export default Link;
->
import useNavigation from "../hooks/use-navigation";
import classNames from "classnames";
function Link({ to, children }){
const {navigate} = useNavigation();
const classes = classNames('text-blue-500');
const handleClick = (event) => {
if(event.metaKey || event.ctrlKey){ // ctrl 키 눌렀을 때 새로운 탭에서 열기
return;
}
event.preventDefault();
navigate(to);
}
return <a className={classes} href={to} onClick={handleClick}>{children}</a>
}
export default Link;
그럼 기존과 같이 코드는 잘 작동한다!
반응형
'React' 카테고리의 다른 글
| [React] Navigation 만들기 (8) - SideBar 현재페이지일때 스타일링주기 (0) | 2023.07.24 |
|---|---|
| [React] Navigation 만들기 (7) - SideBar 컴포넌트 만들기 (0) | 2023.07.22 |
| [React] Navigation 만들기 (5) - Route 컴포넌트 만들기 (0) | 2023.07.22 |
| [React] Navigation 만들기 (4) - 스타일링 보강하기 (0) | 2023.07.22 |
| [React] Navigation 만들기 (3) - Link 기능 보강하기 (새탭에서 열기) (0) | 2023.07.22 |
Comments