This repository was archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathLink.js
More file actions
75 lines (71 loc) · 2.18 KB
/
Link.js
File metadata and controls
75 lines (71 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, { Component } from 'react';
import { withNavigation, NavigationActions } from '@react-navigation/core';
const queryString = require('query-string');
const getTopNavigation = navigation => {
const parent = navigation.dangerouslyGetParent();
if (parent) {
return getTopNavigation(parent);
}
return navigation;
};
class LinkWithNavigation extends Component {
render() {
const {
children,
params,
routeName,
routeKey,
navigation,
action,
anchorProps,
} = this.props;
const topNavigation = getTopNavigation(navigation);
const topRouter = topNavigation.router;
const navAction =
action ||
NavigationActions.navigate({
routeName,
key: routeKey,
params,
});
if (!action && !routeName && !routeKey) {
throw new Error(
'Must provide a routeName, routeKey, or a navigation action prop to <Link>'
);
}
if (action && routeKey) {
throw new Error(
'Cannot specify a conflicting "routeKey" and a navigation "action" prop. Either use routeName with routeKey to specify a navigate action, or provide the specific navigation "action" prop.'
);
}
if (action && routeName) {
throw new Error(
'Cannot specify a conflicting "routeName" and a navigation "action" prop. Either use routeName with routeKey to specify a navigate action, or provide the specific navigation "action" prop.'
);
}
const navActionResponse = topRouter.getStateForAction(
navAction,
topNavigation.state
);
const nextState =
navActionResponse === null ? topNavigation.state : navActionResponse;
const pathAndParams = topRouter.getPathAndParamsForState(nextState);
const href = Object.keys(pathAndParams.params).length
? `/${pathAndParams.path}?${queryString.stringify(pathAndParams.params)}`
: `/${pathAndParams.path}`;
return (
<a
href={href}
onClick={e => {
navigation.dispatch(navAction);
e.preventDefault();
}}
{...anchorProps}
>
{children}
</a>
);
}
}
const Link = withNavigation(LinkWithNavigation);
export default Link;