0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【React】react routerを使用してコンポーネントを切り替えたいけど、画面が表示されなかった

Posted at

経緯

react routerを使用してコンポーネントを切り替えるようにしたくて以下のような実装をしていました。

App.tsx

<main>
<BrowserRouter>
    <Routes>
    <Route path='/pageA' element={<A />} />
    <Route path='/movie' element={<B />} />
    </Routes>
</BrowserRouter>
</main >

しかしこの状態では、いざ画面を表示してみるとエラーがdevツールに出力されており、何も表示されません。

TypeError: Cannot destructure property 'basename' of 'm.useContext(...)' as it is null.

解決

少し調べてみるとBrowserRouterはアプリケーション全体をラップするようにしないといけないようです。
main.tsxApp.tsxBrowserRouterでラップするようにすると・・・

main.tsx

<BrowserRouter>
    <App />
</BrowserRouter>
App.tsx

<main>
    <Routes>
    <Route path='/pageA' element={<A />} />
    <Route path='/movie' element={<B />} />
    </Routes>
</main >

うまく画面表示されました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?