[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: 500, width: 500 }}
        />
      ) : 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;

原文地址:https://www.cnblogs.com/Answer1215/p/11431567.html

时间: 2024-08-29 22:30:06

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

React Native Android原生模块开发实战|教程|心得|如何创建React Native Android原生模块

尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 前言 一直想写一下我在React Native原生模块封装方面的一些经验和心得,来分享给大家,但实在抽不开身,今天看了一下日历发现马上就春节了,所以就赶在春节之前将这篇博文写好并发布(其实是两篇:要看iOS篇的点这里<React Native iOS原生模块开发>). 我平时在用React Native开发App时会

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

尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 告诉大家一个好消息.为大家精心准备的React Native视频教程公布了,大家现能够看视频学React Native了. 前言 一直想写一下我在React Native原生模块封装方面的一些经验和心得.来分享给大家,但实在抽不开身.今天看了一下日历发现立即就春节了.所以就赶在春节之前将这篇博文写好并公布(事实上是两篇

[GraphQL] Apollo React Query Component

In this lesson I refactor a React component that utilizes the graphql higher-order component to the new Query render prop component baked into react-apollo. Additional Resources: What's next for React Apollo import React from "react"; import { r

[GraphQL] Apollo React Mutation Component

In this lesson I refactor a React component that utilizes a higher-order component for mutations to the new Mutation render prop component baked into react-apollo 2.1. Additional Resources: https://dev-blog.apollodata.com/introducing-react-apollo-2-1

React Native学习笔记(一)Mac OS X下React Native的环境搭建

本文介绍Mac OS X系统下的React Native环境搭建过程. 1.环境要求: 1) Mac OS X操作系统 2) Xcode6.4或以上版本 3) Node.js4.0或以上版本 4) watchman和flow 2.安装过程 1) Node.js的安装可以在Node.js的官网https://nodejs.org/ 中下载最新的版本.这里下载的是node-v4.4.2.pkg版本.直接双击运行安装就可以了.查看是否安装成功可以在终端中输入如下命令: $node -v 如果能够显示版

React Native iOS原生模块开发实战|教程|心得|如何创建React Native iOS原生模块

尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691432) 前言 一直想写一下我在React Native原生模块封装方面的一些经验和心得,来分享给大家,但实在抽不开身,今天看了一下日历发现马上就春节了,所以就赶在春节之前将这篇博文写好并发布(其实是两篇:要看Android篇的点这里<React Native Android原生模块开发>). 我平时在用React Nativ

[React] Call setState with null to Avoid Triggering an Update in React 16

Sometimes it’s desired to decide within an updater function if an update to re-render should be triggered. Calling .setState with null no longer triggers an update in React 16. This means we can decided if the state gets updated within our .setState 

[React] Refactor a Stateful List Component to a Functional Component with React PowerPlug

In this lesson we'll look at React PowerPlug's <List /> component by refactoring a normal class component with state and handlers to a functional component powered by React PowerPlug. import React from "react"; import { render } from "

[React] Return a list of elements from a functional component in React

We sometimes just want to return a couple of elements next to one another from a React functional component, without adding a wrapper component which only purpose would be to wrap elements that we want to render. In this one minute lesson we are goin