首页 > 解决方案 > 如何识别 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="닉네임&quot; 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;

标签: javascriptnode.jsreactjsreact-native

解决方案


首先,我不知道究竟是什么Linking,如果它是一个外部库或其他东西(也许分享一些有关的上下文可能会有所帮助)。

其次,您试图访问nickonuseEffect的回调函数内部,Linking并将其作为 参数的解构对象。检查这一点。

编辑: 我刚刚阅读了有关Linkinghttps://reactnative.dev/docs/linking#handling-deep-links)的文档

您根本没有nickaddEventListener. 只需删除它,它就会获得nickfrom 状态,因为它在全局范围内。

            const [nick, onChangeNick] = useInput('');    

            useEffect(() => {
// Remove `nick`
                Linking.addEventListener('url', async ({url}) => { 
                console.log('nick::', nick);                                               //third


                });
                return () => Linking.removeEventListener('url');
            }, []);

推荐阅读