1
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 1 year has passed since last update.

React + react-router-dom v6 でURLを設定する ( App.js を編集するだけの最小構成 )

Last updated at Posted at 2022-12-29

ポイント

BrowserRouter > Routes > Route の入れ子構造にする

インストール

npm install react-router-dom@6

App.js

App.js を以下の内容にする

import { Routes, Route, BrowserRouter } from 'react-router-dom';

function Hello() {
  return <h2>Hello</h2>;
}

function Home() {
  return <h2>Home</h2>;
}

function App() {
  return (
    <BrowserRouter>
      <h1>Hello React Router v6</h1>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/hello" element={<Hello />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

ブラウザアクセス

/hello で表示が変わるのが分かる

image
image

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

1
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
1
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?