2
2

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.

画面遷移の際に位置をtopに戻す方法【React】

2
Posted at

概要

画面遷移(URL移動)の際に画面の位置をトップに戻したい
ちょっとしたコード書くだけで解決できた
(ちなみにReactで作成したcomponentです)

実装


  this.setState(() => window.scrollTo(0, 0));

Componentのよしななタイミングでこれをセットしてあげると画面位置がトップになります。

簡易な例でいうと、


  componentDidMount() {
    this.setState(() => window.scrollTo(0, 0));
  }

これで画面遷移してきたページで前のページのlocationに影響されずに画面の位置がトップになります。

出典元

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?