1
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 Routerでの <a> と <Link> の違いを理解する

Posted at

はじめに

Reactでアプリを作っている際に、ページ遷移のために
<a><Link> のどちらを使えばいいか迷うことがありました。

この記事では、使い分けを簡単にまとめたいと思います。

<a> を使うとどうなるのか?

通常のHTMLの <a> タグはブラウザに「新しいページを開け」と命令します。

<a href="/map">Go to Map</a>

この場合:

ページ全体がリロード(再読み込み)されます。

Reactアプリの状態(state)がリセットされます。

アプリが再起動するような動作になります。

つまり、SPA(Single Page Application)の「速さ」や「なめらかさ」が失われます。

<Link> を使うとどうなるのか?

React Routerが提供する コンポーネントは、
「ページ遷移をJavaScript側で処理」してくれます。

import { Link } from "react-router-dom";

<Link to="/map">Go to Map</Link>

この場合:

ページはリロードされずにコンポーネントが切り替わる

Reactの状態(state)も保持されたまま

遷移が高速でスムーズ

参考資料

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