Skip to content

feat: added typed route hook#21

Merged
Ivnov merged 1 commit into
masterfrom
feat/add-type-route-hook
May 21, 2025
Merged

feat: added typed route hook#21
Ivnov merged 1 commit into
masterfrom
feat/add-type-route-hook

Conversation

@Ivnov

@Ivnov Ivnov commented May 21, 2025

Copy link
Copy Markdown
Contributor

Base problem

When we need to use typed route we usually add types to stack params:

export type RootStackParamList = {
 fooRoute: { userId: string };
}

then add it as a prop

export type FooRouteProp = RouteProp<
  RootStackParamList,
  'fooRoute'
>;

and, eventually, use as generic type

const { params } = useRoute<FooRouteProp>();

Solution

Add hook-helper for handle this boilerplate

export const useTypedRoute = <T extends keyof RootStackParamList>() =>
  useRoute<RouteProp<RootStackParamList, T>>();

Usage

Add types for stack

export type RootStackParamList = {
 fooRoute: { userId: string };
}

use helper

const { params } = useTypedRoute<'fooRoute'>();

@Ivnov Ivnov requested a review from VladyslavMartynov10 May 21, 2025 11:07
@Ivnov Ivnov merged commit ab5b79c into master May 21, 2025
2 checks passed
@michaelchyhura michaelchyhura added the enhancement New feature or request label May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants