当挂钩的初始值是数据库的查询结果时,出现“比上次渲染期间渲染的挂钩更多”错误

时间:2019-12-14 21:27:09

标签: reactjs react-native react-hooks react-apollo

我正在使用React的钩子,我想拥有一个从数据库中检索到的值作为初始值。但是,出现以下错误:

  

不变违规:不变违规:渲染的钩子比   在上一个渲染期间。

ExternalContext econtext = FacesContext.getCurrentInstance().getExternalContext();
        econtext.responseReset();
        econtext.setResponseContentType(contentType);
        econtext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        econtext.setResponseContentLength(contentLength);

我正在使用React Apollo钩子。

更新

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

1 个答案:

答案 0 :(得分:1)

问题是您正在使用return下面的钩子。尝试更新

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>
    )
}