React(React Hooks)+Redux環境で、初回描画時には検索を実行したいけど
ブラウザバック時には走らせたくないときに書いたコードです。
(もっといい方法があれば教えていただければ嬉しい…)
const isBrowzerBack = React.useRef(false);
// ブラウザ履歴を移動したときのみフラグをセット
window.onpopstate = () => {
isBrowzerBack.current = true;
}
React.useEffect(() => {
if(isBrowzerBack.current) {
isBrowzerBack.current = false;
} else {
searchUser();
}
}, []);