按钮过载问题



早些时候,我从另一个函数getFriendId中调用我的addFriend函数(运行 graphql 突变(。它工作得很好。

const getFriendId = React.useCallback(
(data: UsersLazyQueryHookResult) => {
if (data) {
if (data.users.nodes.length == 0) {
Alert.alert('User Not Found');
} else {
setUserData(data);
setNumberOfUsers(data.users.nodes.length);
//showUsers(data, Number(numberOfUsers));
//addFriend(Number(data.users.nodes[0].id));
}
}
},
[addFriend],
);

但是,现在我从我的 ShowUsers 函数中调用它,该函数首先显示所有返回的用户,并显示每个用户的按钮。我正在尝试这个:

const showUsers = React.useCallback(
(data: UsersLazyQueryHookResult, numberOfUsers: Number) => {
for (var i = 0; i < numberOfUsers; i++) {
const userId = data.users.nodes[i].id;
const userName = data.users.nodes[i].firstName
.concat(' ')
.concat(data.users.nodes[i].lastName);
return (
<View >
<View >
<Text >{userName}</Text>
<Button  
onPress={addFriend(Number(data.users.nodes[0].id))}
>
</Button>
</View>
</View>
);
}
},
[createUserRelationMutation],
);

但是,我在按钮的 on 上遇到问题按:

No overload matches this call.
Overload 1 of 2, '(props: Readonly<Button>): Button', gave the following error.
Type 'void' is not assignable to type '(() => void) | undefined'.
Overload 2 of 2, '(props: Button, context?: any): Button', gave the following error.
Type 'void' is not assignable to type '(() => void) | undefined'.ts(2769)

这是我的addFriend函数:

const addFriend = React.useCallback(
(id: Number) => {
createUserRelationMutation({
variables: {
input: { relatedUserId: id, type: RelationType.Friend, userId: 7 },
},
});
},
[createUserRelationMutation],
);

这就是我在主返回 jsx 中使用showUsers的方式

{showUsers(userData, Number(numberOfUsers))}
onPress={addFriend(Number(data.users.nodes[0].id))}

这将调用addFriend函数而不onPress

所以改变:

onPress={addFriend(Number(data.users.nodes[0].id))}

自:

onPress={() => addFriend(Number(data.users.nodes[0].id))}

相关内容

  • 没有找到相关文章

最新更新