old Title : React useEffect doesn't run on page reload
useEffect(()=>{console.log("I ran")}, []) // should run once
It does run if I navigate to the page that contains the component, but doesn't run again if I reload the page.
How do I get the effect to run even if I reload the page ? Edit: Reloading the page means to me pressing F5
Full Component
const Lobby = () => {
const [result, setResult] = useState();
const { joinedLobby, setCurJoinedLobby } = useContext(JoinedLobbyContext);
useEffect(() => {
if (joinedLobby !== undefined)
sessionStorage.setItem('joinedLobby', JSON.stringify(joinedLobby));
const storedJoinedLobby =
sessionStorage.getItem('joinedLobby') !== null
? JSON.parse(sessionStorage.getItem('joinedLobby'))
: undefined;
if (joinedLobby === undefined && storedJoinedLobby !== undefined) {
setCurJoinedLobby(storedJoinedLobby);
}
if (joinedLobby === undefined) return setResult(<div>Bad Link</div>);
setResult(<div>Some stuff here</div>);
}, []); // will be [joinedLobby] in the future
return <div>{result}</div>
};
CodeSandbox
Cut down example, clone and run locally to get session store