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