反应导航 AddListener 无法正常工作
Posted
技术标签:
【中文标题】反应导航 AddListener 无法正常工作【英文标题】:React Navigation AddListener Not working Correctly 【发布时间】:2022-01-06 01:28:54 【问题描述】:当 url 打开屏幕上的页面时,我正在尝试深度链接和捕获 url,当我的应用程序不在后台运行时,它在功能上有效。但是,如果应用程序在后台运行,则它不起作用。
const isFocused = useIsFocused();
useEffect(() =>
getCode();
, [isFocused]);
const getCode = async () =>
//we will generate a button in the forget password email, link will include a url ===> mobile://auth/new-password?verification=534396
const url = await Linking.getInitialURL();
console.log('url', url);
if (url?.includes('new-password'))
//problem, it may not work if app is still working on the background
const query = queryString.parseUrl(url);
const verifyCode = query.query.verification;
setVerificationCode(String(verifyCode));
setIsLoading(false);
else
Alert.alert('Something went wrong');
;
当我使用链接直接链接到应用程序时,它的控制台日志为“url null”。我的问题是在焦点部分还是在 getInitialUrl 函数上?
【问题讨论】:
【参考方案1】:我遇到了类似的问题。
在我的例子中,我们使用了NavigationContainer 中的linking
,它会打开相同的X
屏幕,不管该屏幕的深层链接数据是否具有不同的值。
我通过在Stack.Screen
上使用getId
修复了它:
const getId = ( params ) => params?.id;
<Stack.Screen name="X" component=XComponent getId=getId />
你可以在getId
这里找到更多信息https://reactnavigation.org/docs/screen/#getid。
【讨论】:
以上是关于反应导航 AddListener 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
反应本机路由器通量:TypeError:未定义不是函数(评估'addListener')
反应原生推送通知 IOS 'addListener 不是函数'