본문 바로가기

프론트엔드 공부/react

history.push(경로,{})

history.push 로 넘어가는 경로 페이지에 오브젝트를 넘겨주고, location을 props 로 받아 쓸수 있음,

단 해당경로페이지 라우팅이 "/페이지/:id"와 같이 파라미터를 받아야하는 페이지.js 인 경우 

const userId = location.state.userId로 상위에 정의 내려 버리면 

하위에 들어가는 모든 페이지 (ex. '/페이지/1','/페이지/2','/페이지/3'...) 에서 모두 userId를 찾기 때문에 오류 발생하므로

원하는 페이지에 직접 {location.state.userId}로 정의 해주는 것 이좋음.

 

imort {useHistory} from react-router-dom;

const App = () =>{
const history = useHistory();
const historyPush = () =>{
history.push(경로,{object.key:object.value})
}

return(
<div>

</div>)
}

export default App;

//해당 경로 페이지 

const Page= ({location}) =>{
const userId = location;
console.log(userId)
return(
<div>

</div>)
}
export default Page;