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 | 
                            Tags
                            
                        
                          
                          - MongoDB
 - nodejs
 - Docker Swarm
 - RAID
 - 네트워크
 - 쿠버네티스
 - Javascript
 - PAT
 - RAPA
 - node.js
 - kubernetes
 - docker
 - OpenStack
 - 이론
 - IaaS
 - mysql
 - 명령어
 - 실습
 - gns3
 - express
 - 클라우드
 - network
 - 도커
 - 용어정리
 - PaaS
 - worker
 - 개념
 - Docker-compose
 - git
 - dockerfile
 
                            Archives
                            
                        
                          
                          - Today
 
- Total
 
융융이'Blog
REACT_SPA페이지구현(2)(match,location,history,withrouter) 본문
match,location,history
리액트에서 각 컴포넌트에서 props처럼 받는 데이터가 match, location,history에 들어 있다.
import React from 'react';
import queryString from 'query-string';
const About = ({location, match, history}) => {
  const query = queryString.parse(location.search);
  console.log(location);
  console.log(match);
  console.log(history);
  ...
- location
 
{pathname: "/about", search: "?key=value&key2=value2", hash: "", state: undefined}
location은 현재 페이지의 주소 상태를 알려줍니다.
- match
 
{path: "/about/:name?", url: "/about", isExact: true, params: {…}}
match는 <Route>컴포넌트에서 설정한 path와 관련된 데이터들을 조회할 때 사용합니다.
- history
 
About.js:7 {length: 6, action: "POP", location: {…}, createHref: ƒ, push: ƒ, …}
history는 현재 라우터를 조작할 때 사용합니다. 예를 들어 뒤쪽 페이지로 넘어가거나, 다시 앞쪽 페이지로 가거나, 새로운 주소로 이동해야 할 때 이 객체가. 지닌 함수들을 호출합니다.
위 세가지 props는 라우트로 사용된 컴포넌트에서만 접근할 수 있었습니다. 라우트 외부에 있는 라우터는 props를 사용하고 싶을 때 어떻게 할까요?
withRouter
import React from 'react';
import { NavLink, withRouter } from 'react-router-dom';
const Menu = () => {
  const activeStyle = {
    color: 'green',
    fontSize: '2rem'
  };
  return (
    <div>
      <ul>
        <li><NavLink exact to="/" activeStyle={activeStyle}>홈</NavLink></li>
        <li><NavLink exact to="/about" activeStyle={activeStyle}>소개</NavLink></li>    
        <li><NavLink to="/about/react" activeStyle={activeStyle}>REACT 소개</NavLink></li>
      </ul>
    </div>
  )
}
export default withRouter(Menu);
withRouter을 이용하여 export default withRouter(Menu)처럼 withRouter을 감싸주면 위 세가지의 props를 사용할 수 있습니다.
'2022이전 > React' 카테고리의 다른 글
| REACT_FRONT_END 의 폴더구성 (0) | 2020.02.03 | 
|---|---|
| REACT_코드스플리팅_Code Spliting (1) | 2020.01.28 | 
| SPA 페이지 구현(1)(react-router-dom, NODE_PATH, BrowserRouter,Route,Query-String) (0) | 2020.01.28 | 
| REACT_API호출(redux-promise-middleware,redux-pender) (0) | 2020.01.27 | 
| REACT_REDUX\_미들웨어(redux-logger, redux-thunk) (0) | 2020.01.27 |