Typesafe Stack & Navigation
es-lynn opened this issue · 1 comments
I was able to find a way to get both Stack
and navigate
to be typesafe for the URL, component & props with minimal boilerplate.
In the demo above, you can see the following being demonstrated:
- Only a simple type mapper
Routes
is required to map URLs to Components. <TypesafeScreen>
knows that only keys inRoutes
are valid URLs.<TypesafeScreen>
disallows components with incompatible props to be passed in.navigate
knows that only keys inRoutes
are valid URLs.navigate
knows what props should be passed in based on the URL
Would the react-navigation team be interested in implementing such a feature into the core library? If yes, then I can work on a PR.
Thanks for the RFC, it looks cool but has a problem compared to the current method in the docs: your navigate
function seems global, but each screen should have its own navigation
prop with proper types, for example navigation.setParams
, navigation.push
etc. should also have correct types, not just navigate
.
Basically, all screens in a navigator needs to be aware of params of other screens (without a global navigate
function), so just specifying params for the screen won't be sufficient. That's why current way suggests to define all params in one place and then use that in each screen.