示例
import React, { useState, useEffect } from "react";
import { Linking, StyleSheet, Text, View } from "react-native";
const useMount = func => useEffect(() => func(), []);
const useInitialURL = () => {
const [url, setUrl] = useState(null);
const [processing, setProcessing] = useState(true);
useMount(() => {
const getUrlAsync = async () => {
const initialUrl = await Linking.getInitialURL();
setTimeout(() => {
setUrl(initialUrl);
setProcessing(false);
}, 1000);
};
getUrlAsync();
});
return { url, processing };
};
const App = () => {
const { url: initialUrl, processing } = useInitialURL();
return (
<View style={styles.container}>
<Text>
{processing
? `Processing the initial url from a deep link`
: `The deep link is: ${initialUrl || "None"}`}
</Text>
</View>
);
};
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: "center", alignItems: "center" },
});
export default App;