score:2

Accepted answer

paramavailability is false by default. useeffect is first called after initial render. so you always redirect.

to fix your hoc just extract checking logic to a separate function and use it to set the default state:

export default (wrappedcomponent, queryparamkey) => {
   return props => {

    function checkparam() {
        const urlparams = new urlsearchparams(document.location.search);
        const token = urlparams.get(queryparamkey);
        return !!token;
    }

   const [ paramavailability, setparamavailability ] = usestate(checkparam());


  useeffect(() => {
    setparamavailability(checkparam());
  });

  return paramavailability ? (
    <wrappedcomponent {...props} />
  ) : <redirect to="/404" />;
  };
};

Related Query

More Query from same tag