-
Notifications
You must be signed in to change notification settings - Fork 4
/
LinkedRouter.js
39 lines (31 loc) · 1003 Bytes
/
LinkedRouter.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
import React from 'react';
import { Linking } from 'react-native';
import { Router } from 'react-native-router-flux';
import crossroads from 'crossroads';
export default class LinkedRouter extends React.Component {
constructor(props) {
super(props);
this.handleOpenURL = this.handleOpenURL.bind(this);
}
componentDidMount() {
Linking
.getInitialURL()
.then(url => this.handleOpenURL({ url }))
.catch(console.error);
Linking.addEventListener('url', this.handleOpenURL);
}
componentWillUnmount() {
Linking.removeEventListener('url', this.handleOpenURL);
}
handleOpenURL(event) {
if (event.url && event.url.indexOf(this.props.scheme + '://') === 0) {
crossroads.parse(event.url.slice(this.props.scheme.length + 3));
}
}
render() {
return <Router { ...this.props }/>;
}
}
LinkedRouter.propTypes = {
scheme: React.PropTypes.string.isRequired
};