javascript - 如何识别 Linking.addEventListener 中的数据?
问题描述
首先,我在输入中输入昵称。二、通过Linking.openURL('http://10.0.2.2:3065/auth/kakao')获取url数据。第三,当 Linking.addEventListener 执行时,我想让我输入的 nick 记录在 console.log("nick:",) 中。
但是当我运行我的代码时,它会写 nick: undifned。
我怎样才能修复我的代码?
这是我的代码
const Kakao = () => {
const [nick, onChangeNick] = useInput('');
useEffect(() => {
Linking.addEventListener('url', async ({url, nick}) => {
console.log('nick::', nick); //third
});
return () => Linking.removeEventListener('url');
}, []);
return (
<Container>
<Inputs placeholder="닉네임" value={nick} onChange={onChangeNick} /> //first
<LoginButton
onPress={() => Linking.openURL('http://10.0.2.2:3065/auth/kakao')}> //second
<Label>카카오 로그인 링크</Label>
</LoginButton>
</Container>
);
};
export default Kakao;
解决方案
首先,我不知道究竟是什么
Linking
,如果它是一个外部库或其他东西(也许分享一些有关的上下文可能会有所帮助)。其次,您试图访问
nick
onuseEffect
的回调函数内部,Linking
并将其作为 参数的解构对象。检查这一点。
编辑:
我刚刚阅读了有关Linking
(https://reactnative.dev/docs/linking#handling-deep-links)的文档
您根本没有nick
在addEventListener
. 只需删除它,它就会获得nick
from 状态,因为它在全局范围内。
const [nick, onChangeNick] = useInput('');
useEffect(() => {
// Remove `nick`
Linking.addEventListener('url', async ({url}) => {
console.log('nick::', nick); //third
});
return () => Linking.removeEventListener('url');
}, []);
推荐阅读
- asp.net-core - ASP.NET Core 禁用 datetime-local 的时间选择器
- pandas - 以正确的格式读取日期
- javascript - 加载没有“新图像()”的图像
- gradle - 从 gradle 中的几个冲突的传递依赖项中强制执行最高版本
- css - Is there a way to keep the content inside a mat-card responsive?
- python - Filtering languages queryset looping on tuple (code, name_translated)
- r - 如何验证开源 R 包;即消除恶意包
- php - 如何使用 PHP 和 MYSQL 在表中插入多行?
- sql - Dtd 或 Xsd Sql 定义
- batch-file - 使用包含 10,000 个项目的列表时批处理文件崩溃