Skip to content

Commit

Permalink
feat: support navigate-like object in Link
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed May 9, 2021
1 parent b28bfdd commit 1478659
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
10 changes: 9 additions & 1 deletion example/src/Screens/LinkComponent.tsx
Expand Up @@ -24,7 +24,8 @@ const scrollEnabled = Platform.select({ web: true, default: false });
const LinkButton = ({
to,
...rest
}: React.ComponentProps<typeof Button> & { to: string }) => {
}: React.ComponentProps<typeof Button> &
Parameters<typeof useLinkProps>[0]) => {
const props = useLinkProps({ to });

return <Button {...props} {...rest} />;
Expand Down Expand Up @@ -57,6 +58,13 @@ const ArticleScreen = ({
>
Go to /link-component/music
</LinkButton>
<LinkButton
to={{ screen: 'Home' }}
mode="contained"
style={styles.button}
>
Go to /
</LinkButton>
<Button
mode="outlined"
onPress={() => navigation.goBack()}
Expand Down
3 changes: 2 additions & 1 deletion packages/native/src/Link.tsx
Expand Up @@ -2,9 +2,10 @@ import * as React from 'react';
import { Text, TextProps, GestureResponderEvent, Platform } from 'react-native';
import type { NavigationAction } from '@react-navigation/core';
import useLinkProps from './useLinkProps';
import type { To } from './useLinkTo';

type Props = {
to: string;
to: To;
action?: NavigationAction;
target?: string;
onPress?: (
Expand Down
12 changes: 2 additions & 10 deletions packages/native/src/useLinkProps.tsx
Expand Up @@ -4,10 +4,10 @@ import {
NavigationAction,
NavigationHelpersContext,
} from '@react-navigation/core';
import useLinkTo from './useLinkTo';
import useLinkTo, { To } from './useLinkTo';

type Props = {
to: string;
to: To;
action?: NavigationAction;
};

Expand Down Expand Up @@ -49,14 +49,6 @@ export default function useLinkProps({ to, action }: Props) {
throw new Error("Couldn't find a navigation object.");
}
} else {
if (typeof to !== 'string') {
throw new Error(
`To 'to' option is invalid (found '${String(
to
)}'. It must be a valid string for navigation.`
);
}

linkTo(to);
}
}
Expand Down
50 changes: 35 additions & 15 deletions packages/native/src/useLinkTo.tsx
Expand Up @@ -6,37 +6,57 @@ import {
} from '@react-navigation/core';
import LinkingContext from './LinkingContext';

export type To<
ParamList extends ReactNavigation.RootParamList = ReactNavigation.RootParamList,
RouteName extends keyof ParamList = keyof ParamList
> =
| string
| (undefined extends ParamList[RouteName]
? {
screen: RouteName;
params?: ParamList[RouteName];
}
: {
screen: RouteName;
params: ParamList[RouteName];
});

export default function useLinkTo() {
const navigation = React.useContext(NavigationContext);
const linking = React.useContext(LinkingContext);

const linkTo = React.useCallback(
(path: string) => {
if (!path.startsWith('/')) {
throw new Error(`The path must start with '/' (${path}).`);
}

(to: To) => {
if (navigation === undefined) {
throw new Error(
"Couldn't find a navigation object. Is your component inside a screen in a navigator?"
);
}

let root = navigation;
let current;

// Traverse up to get the root navigation
while ((current = root.getParent())) {
root = current;
}

if (typeof to !== 'string') {
root.navigate(to.screen, to.params);
return;
}

if (!to.startsWith('/')) {
throw new Error(`The path must start with '/' (${to}).`);
}

const { options } = linking;

const state = options?.getStateFromPath
? options.getStateFromPath(path, options.config)
: getStateFromPath(path, options?.config);
? options.getStateFromPath(to, options.config)
: getStateFromPath(to, options?.config);

if (state) {
let root = navigation;
let current;

// Traverse up to get the root navigation
while ((current = root.getParent())) {
root = current;
}

const action = getActionFromState(state, options?.config);

if (action !== undefined) {
Expand Down

0 comments on commit 1478659

Please sign in to comment.