gpt4 book ai didi

reactjs - "Rendered more hooks than during the previous render"当钩子(Hook)的初始值是来自数据库的查询结果时出错

转载 作者:行者123 更新时间:2023-12-04 11:37:56 31 4
gpt4 key购买 nike

我正在使用 React 的钩子(Hook),我希望有一个从数据库中检索到的值作为初始值。但是,我收到以下错误:

Invariant Violation: Invariant Violation: Rendered more hooks than during the previous render.



const { data, loading, error } = useQuery(GET_DATA)
const { initialValue } = data
const [value, setValue] = useState(initialValue)

我正在使用 React Apollo 钩子(Hook)。

更新

export default NotificationScreen = ({ navigation }) => {
const { data: initialNotificationSettings, loading: loadingInitialSettings, error: initialSettingsError } = useQuery(GET_NOTIFICATION_SETTINGS)
if (loadingInitialSettings) {
return (
<View style={[styles.container, styles.horizontal]}>
<ActivityIndicator size="large" color="#FF5D4E" />
</View>
)
}
if (initialSettingsError) return <Text>Error...</Text>

const {
borrowerLendingNotificationToken,
} = initialNotificationSettings.me
const [borrowerPending, notifyBorrowerPending] = useState(borrowerLendingNotificationToken)

return (
<SafeAreaView style={styles.container}>
</SafeAreaView>
)
}

最佳答案

问题是您在返回下方使用钩子(Hook)。尝试更新

export default NotificationScreen = ({ navigation }) => {
const { data: initialNotificationSettings, loading: loadingInitialSettings, error: initialSettingsError } = useQuery(GET_NOTIFICATION_SETTINGS)

const [borrowerPending, notifyBorrowerPending] = useState("")

useEffect(() => {
if (initialNotificationSettings.me) {
const { borrowerLendingNotificationToken } = initialNotificationSettings.me
notifyBorrowerPending(borrowerLendingNotificationToken);
}
}, [initialNotificationSettings]);

if (loadingInitialSettings) {
return (
<View style={[styles.container, styles.horizontal]}>
<ActivityIndicator size="large" color="#FF5D4E" />
</View>
)
}
if (initialSettingsError) return <Text>Error...</Text>

return (
<SafeAreaView style={styles.container}>
</SafeAreaView>
)
}

关于reactjs - "Rendered more hooks than during the previous render"当钩子(Hook)的初始值是来自数据库的查询结果时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59339287/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com