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

NavLinkを使ってみた(備忘録)

Posted at

NavLinkとは

<Link>の特別な種類で、それが「アクティブ」か「保留中」か「遷移中」かを知っています。

ナビゲーションメニューでどれが現在選択されているかを表示するのに便利です。

デフォルトでは、<NavLink>コンポーネントがアクティブになるとactiveクラスが追加されるので、CSSを使ってスタイルを設定することができます。

サンプルコード

return (
    <div className={styles['header-nav']}>
      <nav>
        <ListItem>
          <ul>
            <NavLink
              to="/pagea"
              className={({ isActive }) => (isActive ? styles['active'] : '')}
            >
              ページA
            </NavLink>
          </ul>
          <ul>
            <NavLink
              to="/pageb"
              className={({ isActive }) => (isActive ? styles['active'] : '')}
            >
              ページB
            </NavLink>
          </ul>
          <ul>
            <NavLink
              to="/pagec"
              className={({ isActive }) => (isActive ? styles['active'] : '')}
            >
              ページC
            </NavLink>
          </ul>

classNameプロップは通常のclassNameのように動作しますが、リンクのアクティブ状態と保留状態に基づいて適用されるclassNameをカスタマイズするために関数を渡すこともできます。

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