forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Link.js
52 lines (43 loc) · 1.15 KB
/
Link.js
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
import React from "react";
import { TouchableHighlight } from "react-native";
import PropTypes from "prop-types";
import { __RouterContext as RouterContext } from "react-router";
export default class Link extends React.PureComponent {
static defaultProps = {
component: TouchableHighlight,
replace: false
};
handlePress = (event, history) => {
if (this.props.onPress) this.props.onPress(event);
if (!event.defaultPrevented) {
const { to, replace } = this.props;
if (replace) {
history.replace(to);
} else {
history.push(to);
}
}
};
render() {
const { component: Component, to, replace, ...rest } = this.props;
return (
<RouterContext.Consumer>
{context => (
<Component
{...rest}
onPress={event => this.handlePress(event, context.history)}
/>
)}
</RouterContext.Consumer>
);
}
}
const __DEV__ = true; // TODO
if (__DEV__) {
Link.propTypes = {
onPress: PropTypes.func,
component: PropTypes.elementType,
replace: PropTypes.bool,
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
};
}