티스토리 뷰

카테고리 없음

Router-중첩라우팅

철철22 2018. 9. 20. 10:49
반응형

부모

<Route path="/posts" component={Postlist} />

 

 

<Route exact={true} path={`${props.match.url}`render={() => 

<h3>Postlist</h3>/>

<Route path={`${props.match.url}/:postId`component={Post/>

 

 

 

 

 

 

const Postlist= (propsRouteComponentProps<{}>) =>{

return(

<div>

<Route exact={true} path={`${props.match.url}`render={() => <h3>Postlist</h3>/>

<Route path={`${props.match.url}/:postId`component={Post/>  /** Post 호출**/

</div>

);

};

 

 

 

const Post= (propsRouteComponentProps<{ postIdstring}>) =>{

function goNextPost() {

const nextPostId= +props.match.params.postId1;

props.history.push(`/posts/${nextPostId}`);

}

return(

<div>

<h3>Post {props.match.params.postId}</h3>

<button onClick={goNextPost}>페이지 이동</button>

<p>{new URLSearchParams(props.location.search).get('body')}</p>

</div>

);

};

 

 

 

 

 

switch 모듈

 

<Switch>

<Route />

<Route />

</Switch>

 

일반switch 문처럼비교

 

<Route>pathswitchbreak 같은형태=> 순서에유의해야함
 

 

 

switch404 페이지띄우기

 

const NotFound= () =>{

return(

<h3>Not Found</h3>

);

};

 

<Switch>

<Route exact={true} path="/" component={Home/>

<Route path="/intro"render={() => <h3>소개</h3>/>

<Route path="/posts" component={Postlist/>

<Route component={NotFound}/>// 여기가path안맞을경우404띄우게

</Switch>

 

 

 

 

Redirect

기본적으로 replace 방식(history에 남지 않는다)

 

 

constAdmin= () =>{

constisAdmintrue;

returnisAdmin

?<h3>Admin</h3><Redirect to="/"/>;

};

 

<Route path="/admin" component={Admin/>

 

/admin path로 접속시 isAdmin이 true일 때만 아니면 리다이렉션

 

oldpath

 

<Route path="/intro"render={() => <h3>소개</h3>/>

<Redirect from="/about" to="/intro"/>

 

/intro의 예전 path가 /about 였을시 /intro리다이렉트 시켜주는 방법!!!  Switch 안에서만가능

 

 

 

NavLink

 

`to`에 지정한 path와 URL이 매칭 되는 경우

특별한 스타일클래스를 적용 할 수 있는Link

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함