forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeepLinking.js
40 lines (32 loc) · 873 Bytes
/
DeepLinking.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
import React, { Component } from "react";
import PropTypes from "prop-types";
import { Linking } from "react-native";
const regex = /.*?:\/\//g;
class DeepLinking extends Component {
static contextTypes = {
router: PropTypes.shape({
history: PropTypes.shape({
push: PropTypes.func.isRequired
}).isRequired
}).isRequired
};
async componentDidMount() {
const url = await Linking.getInitialURL();
if (url) this.push(url);
Linking.addEventListener("url", this.handleChange);
}
componentWillUnmount() {
Linking.removeEventListener("url", this.handleChange);
}
handleChange = e => {
this.push(e.url);
};
push = url => {
const pathname = url.replace(regex, "");
this.context.router.history.push(pathname);
};
render() {
return this.props.children || null;
}
}
export default DeepLinking;