[React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks
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的更多相关文章
- vulcanjs 开源工具方便快速开发react graphql meteor 应用
vulcan 开源工具方便快速开发react graphql meteor 应用 操作环境mac os 安装 meteor 安装(此安装有点慢,可以通过正确上网解决) curl https://ins ...
- GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频)
GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频) GraphQL + React Apoll ...
- GraphQL + React Apollo + React Hook 大型项目实战(32 个视频)
GraphQL + React Apollo + React Hook 大型项目实战(32 个视频) GraphQL + React Apollo + React Hook 大型项目实战 #1 介绍「 ...
- React + GraphQL 2020 速成课程
React + GraphQL 2020 速成课程 technologies React (to build our user interface) GraphQL (to get and chang ...
- React Native Android原生模块开发实战|教程|心得|怎样创建React Native Android原生模块
尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 告诉大家一个好消息. ...
- [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 ...
- [GraphQL] Apollo React Query Component
In this lesson I refactor a React component that utilizes the graphql higher-order component to the ...
- [GraphQL] Apollo React Mutation Component
In this lesson I refactor a React component that utilizes a higher-order component for mutations to ...
- urql 高度可自定义&&多功能的react graphql client
urql 是一个很不错的graphql client,使用简单,功能强大,通过exchanges 实现了完整的自定义特性 通过urql 的exchanges 我们可以实现灵活的cache策略 参考资料 ...
随机推荐
- 快速搭建ssh项目
环境:oracle11g.myeclipse2014 首先在web项目中添加spring框架 现在已经添加完spring框架了 然后我们开始添加Hibernate框架 到这一步Hibernate框架就 ...
- 剑指offer54:字符流中第一个不重复的字符
1 题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中 ...
- stm32片上ADC转换实验
原理图所示: BAT_DET 接到PB0 引脚,VSYS 是直流3.7V的电压.再来看下103的adc转换和引脚GPIO的关系 我们直接选用ADC1 根据上表格只能使用通道8 下面给我常用的ADC1寄 ...
- Django框架2——模板
django框架2--模板 直接将HTML硬编码到你的视图里却并不是一个好主意: 对页面设计进行的任何改变都必须对 Python 代码进行相应的修改. 站点设计的修改往往比底层 Python 代码的修 ...
- CSS中的position和float
对基础知识再度做个巩固和梳理. 一.position定位 (一):position的属性 1.absolute:生成绝对定位的元素,相对于最近一级定位不是static的父元素来进行定位: 2.rela ...
- Python--类的定义与使用
转载自https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868200407 ...
- 【HC89S003F4开发板】 3串口调试
HC89S003F4开发板串口调试 使用资料自带的demo 主程序 /************************************系统初始化************************ ...
- 【LEETCODE】36、121题,Best Time to Buy and Sell Stock
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- python之闭包、装饰器
一.学习Python的时候发现函数内部,还可以写函数,并且可以返回函数.觉得挺新奇的,主要是在探索装饰器(有点像Java的注解)的时候,发现这个理解还是很主要的,所以这里记录一下. 二.闭包 1)首先 ...
- php批量检测并去除BOM头的代码
开发中会遇到BOM头, 导致程序无法执行. 浏览器返回接口如下图: 去除BOM头解决方法:<?phpini_set('memory_limit','1024M'); function check ...