LoginSignup
0
0

More than 3 years have passed since last update.

react-router 同ページ内で再描画されない解決メモ

Last updated at Posted at 2020-12-05

コピペで動くようなコードじゃないので、具体例思いついたら修正予


import React, {
  useState,
  useEffect
} from 'react';

import { useLocation } from "react-router";

export default function Foo(props) {

  // カレントロケーション
  const location = useLocation();

  // コンポーネントに渡す変数等
  const [bar, setBar] = useState();

  // ロケーションに変更があったら、stateを書き換える
  useEffect(() => {
    setBar();
  }, [location]);

  return (
     <Baz bar={bar} />
  );
}
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