forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeepLinking.js
40 lines (32 loc) · 891 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 from "react";
import { Linking } from "react-native";
import { __RouterContext as RouterContext } from "react-router";
const protocolAndSlashes = /.*?:\/\//g;
class DeepLinking extends React.Component {
push(url) {
const pathname = url.replace(protocolAndSlashes, "");
this.history.push(pathname);
}
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);
};
render() {
return (
<RouterContext.Consumer>
{context => {
this.history = context.history;
return this.props.children || null;
}}
</RouterContext.Consumer>
);
}
}
export default DeepLinking;