【问题标题】:How to implement Search Function in React-GraphQL using React and ApolloClient?如何使用 React 和 ApolloClient 在 React-GraphQL 中实现搜索功能?
【发布时间】:2022-10-06 19:21:05
【问题描述】:

我想使用反应状态创建一个搜索字段。我创建了相关组件并在控制台中看到了过滤后的输出。但我无法获得对主页的更改。你能帮忙吗?

search.js 文件

我可以在控制台中看到我在此文件中过滤的对象,但无法将其发送到主页

import { useQuery } from \'@apollo/client\';
import React, { useState } from \'react\';
import { GET_CHARACTER } from \'./queries\';


function Search() {
  const setSearchBtn = e => {
    e.preventDefault();
  };
  const [searchFilter, setSearchFilter] = useState(\'\');

  const { loading, error, data } = useQuery(GET_CHARACTER, {
    variables: {
      characterName: searchFilter,
    },
  });
  return (
    <div>
      <form
        className={`${styles.search} d-flex flex-sm-row flex-column align-items-center justify-content-center gap-4 mb-5`}
      >
        <input
          onChange={e => setSearchFilter(e.target.value)}
          placeholder=\"Search for characters\"
          className={styles.input}
          type=\"text\"
        />
        <button
          onClick={setSearchBtn}
          className={`${styles.btn} btn btn-primary fs-5`}
        >
          Search
        </button>
      </form>
    </div>
  );
}



询问

import { gql } from \'@apollo/client\';

export const GET_CHARACTER = gql`
  query getCharacterWithName($characterName: String) {
    characters(filter: { name: $characterName }) {
      results {
        id
        name
        status
        species
        type
        gender
        origin {
          name
        }
        location {
          name
        }
        image
      }
    }
  }
`;

  • 请将您的代码添加为代码块而不是屏幕截图

标签: reactjs graphql react-state


【解决方案1】:

你解决了这个问题吗?谢谢你

【讨论】:

猜你喜欢
  • 2019-09-17
  • 2019-05-06
  • 1970-01-01
  • 2019-11-04
  • 2022-07-27
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多