【发布时间】:2021-05-19 20:32:34
【问题描述】:
我无法在 React Native 的 useState 编码中更新状态。 组件的样式为 TextInput。 我做错了什么状态看不到来自 SearchField 的文本输入?
export const TabOneScreen: FC<IProps> = ({ navigation }) => {
const [userName, setUserName] = useState("jan");
useEffect(() => {
fetch(`https://api.github.com/users/${userName}`)
.then((response) => response.json())
.then((json) => console.log(json));
}, [userName]);
const handleUserName = (value: string) => {
setUserName(value);
};
return (
<StyledContainer>
<SearchField onChangeText={(value: string) => handleUserName(value)} />
<Text>{userName}</Text>
<DetailsField backgroundColor={colors.whiteColor} />
<Button
color={colors.buttonBackground}
title="Show more"
onPress={() => {
navigation.navigate("DetailsScreen");
}}
/>
</StyledContainer>
);
};
【问题讨论】:
-
您从哪里获得 SearchField 组件?
-
辅助您的问题,但您可能不想在每次
userName更改时触发提取。您应该添加某种限制和条件检查并取消任何未完成的请求。 -
上帝输入,我会在 500ms 后调用 setTimeout 来调用 fetch 没有类型...谢谢