forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Link.js
46 lines (39 loc) · 1002 Bytes
/
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
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 = {
component: PropTypes.func,
replace: PropTypes.bool,
to: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object
])
}
static defaultProps = {
component: TouchableHighlight,
replace: false
}
handlePress = () => {
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