with TypeScript
홈
소개
https://hudson.io
https://hudson.io/intro
일반 정적 웹페이지
홈
홈
https://hudson.io
https://hudson.io/intro
Single Page Application
홈
소개
https://hudson.io
https://hudson.io/intro
React with Router
import * as React from 'react';
import './App.css';
import { BrowserRouter as Router, Route } from 'react-router-dom';
const logo = require('./logo.svg');
class App extends React.Component<{}, {}> {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<Router>
<Route path="/" render={() => <h3>Home</h3>}/>
</Router>
</div>
);
}
}
export default App;
항상 렌더링
주소에따라 렌더링
<Router>
<div>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/intro">소개</Link></li>
</ul>
</nav>
<Route path="/" exact={true} render={() => <h3>Home</h3>}/>
<Route path="/intro" render={() => <p>안녕하세요. 이현섭입니다.</p>}/>
</div>
</Router>
Basic Routing
URL Parameter Routing
<Route path="/post/:postId" component={Post}/>
const Post = (props: RouteComponentProps<{ postId: string }>) => {
return (
<h3>Post: {props.match.params.postId}</h3>
);
};
Route Component Props
/post/2?a=b&c=d#hash
const Post = (props: RouteComponentProps<{ postId: string }>) => {
function goNextPost() {
const currPostId = props.match.params.postId;
const nextPostId = +props.match.params.postId + 1 + '';
const { pathname } = props.location;
const nextPath = pathname.replace(currPostId, nextPostId);
props.history.replace(nextPath);
}
return (
<div>
<h3>Post {props.match.params.postId}</h3>
<p>{new URLSearchParams(props.location.search).get('body')}</p>
<button onClick={goNextPost}>Next post</button>
</div>
);
};
const PostList = (props: RouteComponentProps<{}>) => {
return (
<div>
<Route exact={true} path={props.match.url} render={() => <h3>포스트 목록</h3>}/>
<Route path={`${props.match.url}/:postId`} component={Post}/>
</div>
);
};
<Route path="/intro" render={() => <p>소개</p>}/>
<Route path="/:postId" component={Post}/>
소개
https://hudson.io/intro
<Switch>를 사용하지 않은 경우
Post
소개
https://hudson.io/intro
<Switch>로 감싼 경우
<Switch>
<Route path="/intro" render={() => <p>소개</p>}/>
<Route path="/:postId" component={Post}/>
</Switch>
<Switch>
<Route path="/" exact={true} render={() => <h3>Home</h3>}/>
<Route path="/intro" render={() => <p>안녕하세요. 이현섭입니다.</p>}/>
<Route path="/post/:postId" component={Post}/>
<Route render={() => <h3>Not found</h3>}/>
</Switch>
const Admin = () => {
const isAdmin = false; // 임의로 설정
return isAdmin
? <h3>Admin</h3>
: <Redirect to="/"/>;
};
<Switch>
<Route path="/" exact={true} render={() => <h3>Home</h3>}/>
<Route path="/intro" render={() => <p>안녕하세요. 이현섭입니다.</p>}/>
<Route path="/admin" render={(props) => <Admin {...props} isAdmin={false}/>}/>
<Redirect from="/about" to="/intro"/>
<Route path="/post/:postId" component={Post}/>
<Route render={() => <h3>Not found</h3>}/>
</Switch>
<nav>
<ul>
<li><NavLink exact={true} to="/" activeStyle={{ fontWeight: 'bold', color: 'blue' }}>Home</NavLink></li>
<li><NavLink to="/intro" activeStyle={{ fontWeight: 'bold', color: 'blue' }}>소개</NavLink></li>
<li><NavLink to="/admin" activeStyle={{ fontWeight: 'bold', color: 'blue' }}>Admin</NavLink></li>
</ul>
</nav>