일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- db
- 안드로이드
- Algorithm
- vscode
- Database
- git
- react
- 알고리즘
- TypeScript
- VIM
- 프로그래머스
- CS
- Javascript
- 자바
- Data Structure
- 그레이들
- 리트코드
- Python
- LeetCode
- 다이나믹 프로그래밍
- network
- DP
- DFS
- 백준
- java
- BFS
- 동적 계획법
- Redux
- frontend
- Graph
Archives
- Today
- Total
늘 겸손하게
React - react-router-dom - useHistory + 데이터 전달 본문
다른 페이지로 리다이렉트하는 기능을 history.push로 구현해보겠습니다.
1. react-router-dom 설치
1
|
npm i react-router-dom
|
cs |
2. useHistory import
1
|
import { useHistory } from 'react-router-dom';
|
cs |
3. 변수 생성
1
|
const history = useHistory();
|
cs |
4. history.push에 이동할 경로 넣기
1
|
history.push('이동할 경로')
|
cs |
history.push 메소드가 실행되면 현재 경로에 '이동할 경로'가 붙은 경로로 리다이렉션됩니다.
+ history.push 로 이동할 경로에 데이터 전달
state에 데이터를 담아서 이동할 경로에 데이터를 전달할 수 있습니다.
ex)
history.push( {
pathname: '/이동할 경로',
state: {
object: obj
}
})
이동할 경로에서는 useLocation으로 데이터를 읽어들일 수 있습니다.
import {useLocation} from 'react-router-dom';
const location = useLocation();
console.log(location.state.object); // obj 내용 출력됨
'Programming > React' 카테고리의 다른 글
React - 왜 React를 사용하는가? (0) | 2022.07.17 |
---|---|
React - Virtual DOM과 작동원리 (0) | 2022.07.17 |
React - MUI Accordian 스타일 접기/펼치기 버튼 (0) | 2022.07.12 |
React - Module not found: Can't resolve '@mui/material/utils' : (v5, which included a name change.) (0) | 2022.07.12 |
React - 버튼 + Link 컴포넌트 ( 버튼 클릭 -> 다른 페이지로 이동 ) (0) | 2022.07.09 |