Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature suggestion] Add ability to override all hooks on Router component, currently there is just one override possible hook #509

Open
nikita-starostin opened this issue Feb 13, 2025 · 1 comment

Comments

@nikita-starostin
Copy link

I have met the next requirement - there is an existing codebase where lot of urls are hardcoded, e.g.

setLocation('/section1')
setLocation('/section1/:id/subsection');
useRoute('/section1/:id/subsection);

And needed to setup two url schemas based on the turned on feature flag =)

I have used useCustomLocation to do smth like that:

export function useCustomLocation(): HookReturnValue<LocationHook> {
  const [location, setLocation] = useBrowserLocation();
  const featureFlags = useFeatureFlags();
  const appSetLocation = useCallback(async (to: Path, options?: { replace?: boolean }) => {
       const destination = isNewSchemeRequired(to, featureFlags) ? getUrlFromNewScheme(to) : to;
       setLocation(destination, options);
    }
  }, [setLocation]);

  return [location, appSetLocation];
}

<Router hook={useCustomLocation}>
    {/*  when components here call useLocation the useCustomLocation will be used */}
</Router>

That allows to update ALL routing without touching the business code, which is very good for the current case. I would like to do smth like that for useRoute as well, however, have not found in properties of Router ability to override that, so currently I have done:

useSystemRoute(route) {
    const res = useRoute(route);
   // do some custom stuff
   return res;
}

I think extending router with all hooks maybe also beneficial in some cases when need to adjust behavior globally, for example, apply some custom logic on extraction of parameters (useParams), or when migrating some codebases with rules different from wouter conventions to wouter, etc.

@molefrog
Copy link
Owner

useRoute should already be compatible with custom hooks. We are not considering adding more hooks at this time to maintain the library's lightweight nature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants