When using useQuery from Apollo React Hooks, the request will be sent automatically after the component has been mounted. This might not be the desired behaviour as we might want to send the request in response to user action (for instance, after a button click).

In this lesson we're going to learn how to use useLazyQuery hook to execute a query manually in order to fetch dog pictures.

import React, { Fragment, useState } from "react";
import { gql } from "apollo-boost";
import { useLazyQuery } from "@apollo/react-hooks"; const GET_DOGGO = gql`
query Dog($breed: String!) {
dog(breed: $breed) {
id
displayImage
}
}
`; const App = () => {
const [breed, setBreed] = useState("dingo");
const [getDog, { loading, data }] = useLazyQuery(GET_DOGGO); if (loading) {
return <h2>Loading...</h2>;
} return (
<Fragment>
{data && data.dog ? (
<img
alt="Cute Doggo"
src={data.dog.displayImage}
style={{ height: , width: }}
/>
) : null}
<input
type="text"
onChange={event => setBreed(event.target.value)}
placeholder="Choose breed"
/>
<button
onClick={() =>
getDog({
variables: { breed }
})
}
>
Get dog
</button>
</Fragment>
);
}; export default App;

[React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks的更多相关文章

  1. vulcanjs 开源工具方便快速开发react graphql meteor 应用

    vulcan 开源工具方便快速开发react graphql meteor 应用 操作环境mac os 安装 meteor 安装(此安装有点慢,可以通过正确上网解决) curl https://ins ...

  2. GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频)

    GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频) GraphQL + React Apoll ...

  3. GraphQL + React Apollo + React Hook 大型项目实战(32 个视频)

    GraphQL + React Apollo + React Hook 大型项目实战(32 个视频) GraphQL + React Apollo + React Hook 大型项目实战 #1 介绍「 ...

  4. React + GraphQL 2020 速成课程

    React + GraphQL 2020 速成课程 technologies React (to build our user interface) GraphQL (to get and chang ...

  5. React Native Android原生模块开发实战|教程|心得|怎样创建React Native Android原生模块

    尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 告诉大家一个好消息. ...

  6. [React GraphQL] Pass Parameters to urql's useQuery React Hook

    Showing how to use 'uqrl' library to do GraphQL in React. import React, {useState} from 'react' impo ...

  7. [GraphQL] Apollo React Query Component

    In this lesson I refactor a React component that utilizes the graphql higher-order component to the ...

  8. [GraphQL] Apollo React Mutation Component

    In this lesson I refactor a React component that utilizes a higher-order component for mutations to ...

  9. urql 高度可自定义&&多功能的react graphql client

    urql 是一个很不错的graphql client,使用简单,功能强大,通过exchanges 实现了完整的自定义特性 通过urql 的exchanges 我们可以实现灵活的cache策略 参考资料 ...

随机推荐

  1. composer 无法配置命令行写入配置文件问题

    composer config repo.packagist composer https://packagist.phpcomposer.com 这条命令无法修改composer.json 添加中国 ...

  2. DAO工具类的封装源码

    详细源码见下表,绝对原创,转载请注明出处! package com.ydj.util; import java.sql.Connection; import java.sql.PreparedStat ...

  3. 记一次SQL优化

    常见的SQL优化 一.查询优化 1.避免全表扫描 模糊查询前后加%也属于全表扫描 在where子句中对字段进行表达式操作会导致引擎放弃使用索引而进行全表扫描,如: select id from t w ...

  4. DjangoRestful 递归嵌套序列化器实现

    **** 由于博客园不支持markdown语法,所以推荐以下链接阅读: 原创 https://blog.csdn.net/weixin_42495873/article/details/8943354 ...

  5. Django基础之django分页

    一.Django的内置分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views ...

  6. Qt4 和 Qt5 模块的分类

    Qt5 与 Qt4 其中的一个区别是底层架构进行了改变,Qt5 引入了更加详细的模块化的概念,将众多功能细分到几个模块之中,Qt4 则是一种粗略的划分.本文主要对 Qt5 和 Qt4的模块进行一个简单 ...

  7. 解决webpack4.x使用autoprefixer 无效

    安装 npm i webpck webpack-cli style-loader postcss-loader -D 配置 webpack.config.js module: { rules: [{ ...

  8. springboot + shiro 构建权限模块

    权限模块基本流程 权限模块的基本流程:用户申请账号和权限 -->登陆认证 -->安全管控模块认证 -->调用具体权限模块(基于角色的权限控制) --> 登陆成功 -->访 ...

  9. 命令行参数 && json 协议 && 自定义 error 类型

    命令行参数 在写代码的时候,在运行程序做一些初始化操作的时候,往往会通过命令行传参数到程序中,那么就会用到命令行参数 例如,指定程序运行的模式和级别: go run HTTPServer.go --m ...

  10. 一、hystrix如何集成在openfeign中使用

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 HystrixInvocationHandler hystrix是开源的一个熔断组件,s ...