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

More than 5 years have passed since last update.

[Laravel + React.js] React側でページのタイトルを実装する

3
Posted at

導入

Laravelのみでの実装だと、それぞれのページごとのブレードにtitleタグを書けばよかったのですが、
viewをReactにしてしまうと、ルーティングはReact側で行うので、titleもLaravel側ではなく、React側で変更しなくてはなりません。
今回はそれに当たって、react-document-titleというライブラリをインストールしてみたり、、とかなり色々調べたのですが、とても単純に実装できたので、書き残しておきます。

結論をいうと、特別にライブラリを入れる必要はなく、僕のJavaScriptの基礎力のなさを痛感することとなりました...

開発環境

  • Laravel Framework : 6.0.3
  • react : ^16.2.0

実装

用いるのは、document.titleプロパティです。
参考:https://developer.mozilla.org/ja/docs/Web/API/Document/title

これを用いることによって、title要素の中身を更新させることが可能です。

なので、React.jsでは、コンポーネントがレンダリングされた時にtitleを変更させたいので、

componentDidMount() {
    document.title = "TITLE";
}

とすると、ルーティングさせていても、そのページのタイトルを表示させることができます。

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