react-native - 在 react-native 中使用 'useQuery' 加载具有 'apolloClient' 的数据时,“加载”仍然为真

标签 react-native apollo-client

我正在尝试在 React Native 中使用 Apollo 客户端 useQuery 获取数据,但 loading 在真实状态下挂起并且无法获取 data .

下面是实际代码。

App.js

import React from 'react';

import { SafeAreaView, Text, View } from 'react-native';

import { ApolloClient, ApolloProvider, InMemoryCache, gql, useQuery } from '@apollo/client';

const EXCHANGE_RATES = gql`
    query GetExchangeRates {
        rates(currency: "USD") {
            currency
            rate
        }
    }
`;

const TestScreen = () => {
    const { loading, error, data } = useQuery(EXCHANGE_RATES);

    console.log(loading, error, data); // Stop at "true undefined undefined" 🥲

    if (loading) return <Text>Loading...</Text>;
    if (error) return <Text>Error :(</Text>;

    return data.rates.map(({ currency, rate }) => (
        <View key={currency}>
            <Text>
                {currency}: {rate}
            </Text>
        </View>
    ));
};

const apolloClient = new ApolloClient({
    // this uri is the uri provided by the apolloClient official documentation.
    // https://www.apollographql.com/docs/react/get-started#3-connect-your-client-to-react
    uri: 'https://48p1r2roz4.sse.codesandbox.io',
    cache: new InMemoryCache(),
});

const App = () => {
    return (
        <ApolloProvider client={apolloClient}>
            <SafeAreaView>
                <TestScreen />
            </SafeAreaView>
        </ApolloProvider>
    );
};

export default App;

package.json

{
    ...,
    "dependencies": {
        "@apollo/client": "^3.5.10",
        "graphql": "^16.4.0",
        "react": "17.0.2",
        "react-native": "0.68.1",
        "react-native-flipper-apollo-devtools": "^0.0.2"
    },
    "devDependencies": {
        "@babel/core": "^7.12.9",
        "@babel/runtime": "^7.12.5",
        "@graphql-codegen/cli": "^2.6.2",
        "@graphql-codegen/fragment-matcher": "^3.2.1",
        "@graphql-codegen/typed-document-node": "^2.2.8",
        "@graphql-codegen/typescript": "^2.4.8",
        "@graphql-codegen/typescript-apollo-client-helpers": "^2.1.15",
        "@graphql-codegen/typescript-operations": "^2.3.5",
        "@react-native-community/eslint-config": "^2.0.0",
        "@types/jest": "^26.0.23",
        "@types/react-native": "^0.67.3",
        "@types/react-test-renderer": "^17.0.1",
        "@typescript-eslint/eslint-plugin": "^5.17.0",
        "@typescript-eslint/parser": "^5.17.0",
        "babel-jest": "^26.6.3",
        "babel-plugin-module-resolver": "^4.1.0",
        "eslint": "^7.32.0",
        "eslint-config-prettier": "^8.5.0",
        "eslint-config-standard-with-typescript": "^21.0.1",
        "eslint-plugin-import": "^2.26.0",
        "eslint-plugin-node": "^11.1.0",
        "eslint-plugin-promise": "^6.0.0",
        "eslint-plugin-react": "^7.29.4",
        "eslint-plugin-react-hooks": "^4.5.0",
        "eslint-plugin-react-native": "^4.0.0",
        "husky": "^7.0.4",
        "jest": "^26.6.3",
        "lint-staged": "^12.4.1",
        "metro-react-native-babel-preset": "^0.67.0",
        "react-native-flipper": "^0.144.0",
        "react-test-renderer": "17.0.2",
        "typescript": "^4.4.4"
    },
    "resolutions": {
        "@types/react": "^17"
    },
    "jest": {
        "preset": "react-native",
        "moduleFileExtensions": [
            "ts",
            "tsx",
            "js",
            "jsx",
            "json",
            "node"
        ]
    }
}

预期结果:

运行后useQuery()

loading = true, error = undefined, data = undefined
                    ↓
loading = false, error = undefined, data = { ... }

实际结果:

运行后useQuery()

loading = true, error = undefined, data = undefined (Stuck at 'loading = true' 🥲)

我不知道问题是什么。任何帮助将不胜感激。

最佳答案

版本 3.6 使用 React 18 中的 useSyncExternalStore API 实现 useQuery。由于 expo 目前支持 React 17,您可以通过降级 @apollo/client 到版本 3.5.4graphql 到版本 ^16.3.0。您可以找到更多信息here .

关于react-native - 在 react-native 中使用 'useQuery' 加载具有 'apolloClient' 的数据时,“加载”仍然为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72083468/

相关文章:

javascript - 在父组件中的子组件上触发 onPress 事件

javascript - 如何在 React Native 中按下按钮时更改文本值?

vue.js - 更改 Apollo 提供程序中的 Apollo 端点

reactjs - 此版本不符合 Google Play 64 位要求 React Native 应用程序

javascript - Typescript:为通用 React 组件定义未声明的 props

react-native - 尝试运行 React Native ios 应用程序时无法修复这 2 个错误

graphql - 如何将本地 @client 模式扩展添加到 apollo-codegen

javascript - graphql-tag:如何获取正文请求的实际字符串?

graphql - 数据未合并,Apollo 3 分页与字段策略

reactjs - React Apollo Client,加载仅在第一次查询时更改为 true