[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 { render } from "react-dom";
import ApolloClient, { gql } from "apollo-boost";
import { ApolloProvider, Query } from "react-apollo";

const client = new ApolloClient({ uri: "https://graphql-pokemon.now.sh" });

const GET_POKEMON = gql`
  query($name: String!) {
    pokemon(name: $name) {
      name
      image
    }
  }
`;

const Pokemon = ({ name, image }) => {
  return (
    <div>
      <div>{name}</div>
      <img src={image} />
    </div>
  );
};

const PokemonQuery = () => (
  <Query query={GET_POKEMON} variables={{ name: "pikachu" }}>
    {({ loading, error, data }) => {
      if (loading) return <div>Loading...</div>;
      if (error) return <div>Error</div>;
      return <Pokemon name={data.pokemon.name} image={data.pokemon.image} />;
    }}
  </Query>
);

const ApolloApp = () => (
  <ApolloProvider client={client}>
    <PokemonQuery />
  </ApolloProvider>
);

render(<ApolloApp />, document.getElementById("root"));

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

时间: 2024-08-30 16:04:52

[GraphQL] Apollo React Query Component的相关文章

[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 + 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 b

[React] Create component variations in React with styled-components and &quot;extend&quot;

In this lesson, we extend the styles of a base button component to create multiple variations of buttons, using "extend". We can then modify the base styles in one place, and have all button types updated. import React from "react"; im

[React] Update Component State in React With Ramda Lenses

In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. We'll create a lens to focus on the property we want to target and use over to apply the existing state value to a utility function and we'll get back

[React Fundamentals] Component Lifecycle - Updating

The React component lifecycle will allow you to update your components at runtime. This lesson will explore how to do that. import React from 'react'; import ReactDOM from 'react-dom'; export default class App extends React.Component { constructor(){

[React Fundamentals] Component Lifecycle - Mounting Usage

The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson you will learn some simple uses for these hooks. import React from 'react'; import ReactDOM from 'react-dom'; class App extends React.Component { con

[React Fundamentals] Component Lifecycle - Mounting Basics

React components have a lifecycle, and you are able to access specific phases of that lifecycle. This lesson will introduce mounting and unmounting of your React components. import React from 'react'; import ReactDOM from 'react-dom'; export default

A Bite Of React(2) Component, Props and State

component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome extends React.Component { render() { return <h1>Hello, {this.props.name}</h1>; } } 遇到自己定义的component Welcom,React会将它的属性(name)作为对象传递给组建Welcom,即{

[React] Styling a React button component with Radium

React's inline styles allow components to stand on their own by not requiring any external CSS. However HTML's style attributes don't support pseudo selectors like :hover and :active. By using Radium to listen to mouse events we can restore :hover an