如何使用带有 es6 的 React、Graphql 和 Apollo 来过滤元素

How to filter elements using React, Graphql and Apollo with es6

我有一个使用 Apollo 客户端的带有 GraphQL 的 React 项目。我想弄清楚如何根据搜索文本更改查询结果。我在后端实现了查询搜索并且它工作得很好。 但我不知道如何使用相同的查询在 React 中设置过滤器。 尽管有关于如何过滤 https://www.howtographql.com/react-apollo/7-filtering-searching-the-list-of-links/ 的教程,但它不使用 ES6,我真的不知道该怎么做。我在这个过滤器上停留了大约 10 天。

我会告诉你我的代码。

App.js

import React from 'react';
import HeroesDota from './components/HeroesDota';
import Search from './components/HeroSearch'

import { ApolloProvider } from '@apollo/react-hooks';

import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { HttpLink } from "apollo-link-http";


const cache = new InMemoryCache();
const link = new HttpLink({
  uri: "http://localhost:8000/graphql/"
});

const client = new ApolloClient({
  cache,
  link
});


const App = () => {

return (
    <ApolloProvider client={client}>
      <Search />
      <HeroesDota />
    </ApolloProvider>
)};

export default App;

HeroesDota.js(分量)

import React from 'react'
import gql from "graphql-tag";
import { useQuery } from '@apollo/react-hooks';
import '../index.css'
import styled from 'styled-components';

const Images = styled.img`
    margin:0;
    border: 3px solid #288eea;
    display: inline;
    width: 90px;
    height: 50px;
`
const HEROES_DOTA2 = gql`
    query {
        heroes {
            name
            heroType
            image
        }
    }
`;


const HeroesDota = () => {
    const { loading, error, data } = useQuery(HEROES_DOTA2);

    if (loading) return <p>Loading...</p>;
    if (error) return <p>Error :(</p>;

    return data.heroes.map(({name, heroType, image }) => (
      <div className="row" key={Math.random() + 1}>
        <div className="column">
          <button className="button-hero"><Images className="hero_images" src= {`${image}`} alt={name}></Images></button>
            <div className="hero_info">{name} - {heroType}</div>
        </div>
      </div>

    ));
}

export default HeroesDota;

HeroSearch.js(组件未按预期工作)

import React, { useState } from 'react'
import gql from "graphql-tag";
import { withApollo } from 'react-apollo'
import Hero from './HeroesDota'
import '../index.css'

 const SEARCH_HEROES = gql`
     query ($search: String) {
         heroes (search: $search) {
             id
             name
         }
     }
 `;

const Search = () => {

    const [heroes, setHeroes] = useState([])
    const [search, setSearch] = useState('')


    const _executeSearch = async () => {
        const { search } = search
        const result = await this.props.client.query({
          query: SEARCH_HEROES,
          variables: { search },
        })
        const heroes = result.data.heroes.name
        setHeroes({ heroes })
    }

    return (
        <div>
          <div>
            Search
            <input
              type='text'
              onChange={e => setSearch({ search: e.target.value })}
            />
            <button onClick={() => _executeSearch()}>OK</button>
          </div>
          {heroes.map((hero, index) => (
            <Hero key={hero.id} hero={hero} index={index} />
          ))}
        </div>
      )
    }

export default withApollo(Search)

在您按下“确定”按钮执行搜索后,出现以下错误。 未处理的拒绝(ReferenceError):初始化前无法访问'search'。

如果我尝试做类似我在 Component HeroesDota 中做的事情,我仍然做不到。

有谁知道如何使用 es6 在 React 中过滤查询,而不是像他们在本教程中所做的那样基于 class 的组件。

谢谢

const Search = (props) => {
    props.client.query()
    ...
}

const Search = ({ client }) => {
    client.query()
    ...
}

虽然我也建议您使用新的钩子语法。