Apollo重新获取不重新渲染组件

2024-03-13

我正在使用 graphql 从网络服务获取数据,我的客户端代码是这样的

import React, { Component } from 'react';
import {
    Platform,
    StyleSheet,
    Text,
    ActivityIndicator,
    View,
    FlatList,
    TouchableHighlight,
    TextInput,
    Button,
} from 'react-native';
import PropTypes from 'prop-types';
import { graphql, compose } from 'react-apollo';
import ZONES_QUERY from '../graphql/zones.query'


class ZonesScreen extends Component {


    render() {
        const { zones, loading, error } = this.props;


        if (loading) {
            return (
                <ActivityIndicator style={styles.activityIndicator} size='large' />
            )
        } else if (error) {
            return (
                <View style={styles.container}>
                    <Text>Unexcpeted Error</Text>
                    <Button title="retry" onPress={() => { this.props.refetch() }}></Button>

                </View>

            )
        } else {
            return (
                <View
                    style={styles.container}>
                    <FlatList
                        data={zones}
                        renderItem={({ item }) => ZoneRenderItem(item)}
                        keyExtractor={this.keyExtractor}

                    />
                </View>
            )
        }
    }

    //HELPER FUNCTIONS

    keyExtractor = item => item._id;
}

ZonesScreen.propTypes = {
    refetch: PropTypes.func,
}


const zonesQuery = graphql(ZONES_QUERY, {
    options: {
        forceFetch: true,
        fetchPolicy: 'network-only',
        notifyOnNetworkStatusChange: true,
    },
    props: ({ data: { loading, getRoutes, error, refetch } }) => ({
        loading,
        zones: getRoutes,
        refetch,
        error,
    }),
})



const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        backgroundColor: '#eee',
    },
    activityIndicator: {
        flex: 1,
        justifyContent: 'center',
    },
});

export default compose(
    zonesQuery,
)(ZonesScreen)

重新获取不起作用,当我按下重试按钮时,它会得到响应,但组件没有被重新加载。

区域是我从服务中获取的区域列表。

正如你所看到的,我为查询设置了三个选项

强制获取:真实, fetchPolicy: '仅网络', 通知网络状态更改:true,

所有这些都是从 github 存储库读取的

https://github.com/apollographql/apollo-client/issues/1622 https://github.com/apollographql/apollo-client/issues/1622

更新!!!!!!!

我找到了一种使用“react-native-router-flux”中的 Actions 的解决方法,基本上,技巧是使用 Actions.refresh 传递 props 作为参数来强制渲染组件以重新加载它们。见功能refetch = () => {。我还创建了一个refetching告诉数据是否正在重新获取的状态。

import React, { Component } from 'react';
import {
    Platform,
    StyleSheet,
    Text,
    ActivityIndicator,
    View,
    FlatList,
    TouchableHighlight,
    TextInput,
    Button,
} from 'react-native';
import PropTypes from 'prop-types';
import { graphql, compose } from 'react-apollo';
import ZONES_QUERY from '../graphql/zones.query


class ZonesScreen extends Component {

    constructor(props) {
        super(props)
        this.state = { refetching: false }
    }


    render() {
        const { zones, loading, error } = this.props;
        const { refetching } = this.state

        if (loading || refetching) {
            return (
                <ActivityIndicator style={styles.activityIndicator} size='large' />
            )
        } else if (error) {
            return (
                 <View style={styles.container}>
                    <Text>Unexcpeted Error</Text>
                    <Button title="retry" onPress={() => { this.refetch() }}></Button>

                </View>
            )
        } else {
            return (
                <View
                    style={styles.container}>
                    <FlatList
                        data={zones}
                        renderItem={({ item }) => ZoneRenderItem(item)}
                        keyExtractor={this.keyExtractor}
                    />
                </View>
            )
        }
    }

    //HELPER FUNCTIONS

    keyExtractor = item => item._id;

    refetch = () => {
        this.setState({ refetching: true });
        this.props.refetch()
            .then(() => { this.refreshComponent(false, this.props)
        })
            .catch(error => { this.refreshComponent(false, this.props); 
        })
    }

    refreshComponent = (refetching, props) => {
        this.setState({ refetching: refetching });
        Actions.refresh(props)
    }

}


const zonesQuery = graphql(ZONES_QUERY, {
    options:  {
        errorPolicy: 'ignore',
        fetchPolicy: 'network-only',
        notifyOnNetworkStatusChange: true,
    },
    props: ({ data: { loading, getRoutes, error, refetch, } }) => ({
        loading,
        zones: getRoutes,
        error,
        refetch,
    }),
})

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        backgroundColor: '#eee',
    },
    activityIndicator: {
        flex: 1,
        justifyContent: 'center',
    },
});

export default compose(
    zonesQuery,
)(ZonesScreen)

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Apollo重新获取不重新渲染组件 的相关文章

随机推荐

  • 为什么我必须为同一依赖项指定“运行时”和“编译”?

    我依赖于编译和运行应用程序所需的一些工件 根据 Gradle 文档 运行时配置extends编译配置 因此肯定使用添加依赖项runtime暗示着一种隐含的compile依赖性 至少这是我的假设 但它不起作用 当仅依赖于使用的工件时runti
  • 如何在bash中退出if语句而不退出程序?

    重写这个问题以避免更多的反对票 因为对我来说删除它已经太晚了 我正在编写一个脚本 要求用户确认之前sourcing一些其他脚本 为了简化代码 假设有两个脚本sourced 但我希望用户可以source没有或仅是其中一个脚本 不是两者都存在
  • 使用 IE9 支持的 jQuery 旋转 div

    我有一些 div 基本上只是图像 当它们旋转 360 度时我会淡入它们 在所有主流浏览器中一切都可以完美运行 但 IE 除外 有效的 CSS3 代码 box width 400px height 400px display inline b
  • Jquery .Submit() 没有触发提交事件

    我有一个很小的问题 我想在下拉列表更改时使用 jQuery 提交我的代码 mydropDown change function myForm submit 但它没有提交 我还在 Firebug 控制台中触发了以下代码 myForm subm
  • 从子视图中的 UIButton 调用 UIViewController 中的方法

    仍在学习 Objective C 并获得正确的结构 我有一个带有 UIViewController 的 iOS 应用程序 它有一个名为 doSomething 的已定义方法 在我的视图控制器中 我有一个视图 并在该视图中以编程方式创建了许多
  • 为什么在使用空引用访问静态时不会出现 NullPointerException? [复制]

    这个问题在这里已经有答案了 在下面的代码中我们得到的值i在空引用上 尽管NPE不在这里 public class Test static int i 10 Test getTest return null public static voi
  • 安装新版本 XCode 15.0 后无法运行应用程序 XCode

    我已经安装了新版本的 Xcode 15 0 此后我无法运行我的 flutter 应用程序 它向我显示以下错误 Error Xcode DT TOOLCHAIN DIR cannot be used to evaluate LIBRARY S
  • 如何矢量化在较大矩阵的子集上运行函数的代码?

    假设我有以下 9 x 5 矩阵 myArray 54 7 8 1 81 7 55 0 22 5 29 6 92 9 79 4 62 2 17 0 74 4 77 5 64 4 58 7 22 7 18 8 48 6 37 8 20 7 43
  • C 中的释放字符串

    如果我写 char a malloc sizeof char 4 a abc char b abc 我是否需要释放该内存 还是由我的系统完成 在您的情况下 您将无法释放动态分配的内存 因为您正在丢失对它的引用 试试这个 include
  • 通过 Android 共享时 Google+ 应用程序显示错误的图片

    我已以编程方式将 Google 共享添加到我的应用程序中 我有一组照片 我将其包含在我的帖子中并使用以下代码共享 private void shareToGooglePlus Launch the Google share dialog w
  • Python-UDP客户端

    我目前正在阅读Pythonbook https www nostarch com blackhatpython并遇到了以下示例 import socket target host 127 0 0 1 target port 80 creat
  • LD_PRELOAD __libc_start_main 的 Makefile

    我想做的事情很简单 当我启动猫鼬服务器时 我想创建一个额外的线程来完成一些额外的工作 为了做到这一点 我想我需要LD PRELOAD the libc start main服务器的 This is spec hooks cpp typede
  • 与邮递员的 CORS

    这个问题已经被问过几次了 但我还是不明白 当我读到有关的答案时 没有 Access Control Allow Origin 标头 问题 它说应该在请求的服务器上设置一个设置以允许跨域 add header Access Control A
  • 适用于 Windows 的 Composer 安装 -

    所以我尝试在我的 Windows 操作系统上安装 Composer 并设置 Laravel 但是当我运行安装时 我不断收到相同的错误 首先 我选择了 php exe 因为它从目录中询问C wamp bin php php5 4 16 我单击
  • JIT 编译的代码驻留在哪里?

    所以我有这个方法 用Java编写 public void myMethod int y int x 5 y doSomething x 并假设我的应用程序多次调用此函数 当在Java虚拟机上运行该方法的编译代码时 JVM将首先解释该方法 然
  • 如何使用 IF EXIST 条件检查目录或文件是否存在?

    如何检查目录或文件是否存在IF EXIST健康 状况 Such as If exist C Windows OR C Windows2 rem Do something else rem Something else 我该怎么做 简单例子1
  • 应该在带花括号的 return 语句中调用哪个构造函数?

    考虑以下代码 struct NonMovable NonMovable default NonMovable const NonMovable default NonMovable NonMovable delete NonMovable
  • 调度程序 BeginInvoke 语法

    我一直在尝试遵循一些 WCF 数据服务示例并具有以下代码 private void OnSaveCompleted IAsyncResult result Dispatcher BeginInvoke gt context EndSaveC

  • 元素可以有结束标签吗?

    我的同事并不真正了解或理解 html 她的工作是向 CMS 输入信息 我注意到她一直关闭她 hr 像这样的标签 hr 我有谷歌 但我找不到任何地方说这是不允许的或可能会导致问题 我知道它应该是 hr 但值得我告诉她还是这是不必要但有效的标记
  • Apollo重新获取不重新渲染组件

    我正在使用 graphql 从网络服务获取数据 我的客户端代码是这样的 import React Component from react import Platform StyleSheet Text ActivityIndicator