6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[React-router-dom]使い方

Posted at

npmでパッケージ管理している前提です

# パッケージのインストール

$ npm install --save react-router-dom

BrowserRouter,Route,Switchをインポートします。
path指定し、そのpathに当てはまるコンポーネントを指定することで、Reactでのroutingを実現しています。


import React from 'react';
import ReactDOM from 'react-dom';
import {
    BrowserRouter,
    Route,
    Switch,
  } from 'react-router-dom';
import Home from './pages/Home'
import Example from './pages/Example'
import PostEdit from './pages/PostEdit';

function App() {

    return (
        <div>
            <Switch>
                <Route path='/' exact component={Home} />
                <Route path='/posts' exact component={Home} />
                <Route path='/example' exact component={Example} />
                <Route path='/post/edit/:id' exact component={PostEdit} />
            </Switch>
        </div>
    );
}


ReactDOM.render((
    <BrowserRouter>
      <App />
    </BrowserRouter>
  ), document.getElementById('root'))


6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?