简体   繁体   English

Apollo 客户端:如何对自定义事件执行查询

[英]Apollo client: how to execute a query on custom event

Invalid hook call.无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:

  1. You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
  3. You might have more than one copy of React in the same app你可能在同一个应用程序中拥有多个 React 副本

And its rendering twice on handleclick.并且它在handleclick上渲染了两次。

import React, {
  useMemo,
  useState,
  useReducer,
  useEffect,
  useCallback,
  Fragment,
} from 'react';

import getAllFaqs from './faqQuery.graphql';
import getFaqById from './getFaqById.graphql';

import FaqTabIndex from './faqTabIndex';
import FaqCategoryList from './faqCategoryList';

import { useFaq } from '../../peregrine/talons/Faq/useFaq';
import { useFaqById } from '../../peregrine/talons/Faq/useFaqById';

const Faq = (props) => {
  const [faq, setFaq] = useState([]);
  const [faqCategory, setFaqCategory] = useState([]);
  const talonProps = useFaq({
    query: getAllFaqs,
  });
  const { data, error, loading } = talonProps;

  useEffect(() => {
    const tempData = { ...data };
    setFaq([...tempData.faq]);
    setFaqCategory([...tempData.faqCategoryList]);
  }, []);

  const handleClick = (catId) => {
    const talonProps = useFaqById({
      query: getFaqById,
      id: catId,
    });
    const { data, error, loading } = talonProps;

    console.log('====great data', data);
    useEffect(() => {
      const tempData = { ...data };
      setFaq([...tempData.faq]);
    }, [data]);
  };

  const faqList = faq.length ? (
    <FaqTabIndex items={faq} />
  ) : null;
  const catalog = faqCategory ? (
    <FaqCategoryList
      items={faqCategory}
      handleClick={handleClick}
    />
  ) : (
    nul
  );

  console.log('Thank Faq', faq);
  console.log('Thank Category ', faqCategory);
  return (
    <Fragment>
      {faqList}
      {catalog}
    </Fragment>
  );
};

export default Faq;

It seems like you need to execute query when button is clicked, instead of executing it instantly.似乎您需要在单击按钮时执行查询,而不是立即执行它。 That behavior can be implemented by using lazy query .该行为可以通过使用惰性查询来实现。

Try something like this:尝试这样的事情:

../../peregrine/talons/Faq/useFaqById.jsx ../../peregrine/talons/Faq/useFaqById.jsx


import { useLazyQuery } from '@apollo/react-hooks';
import getFaqById from 'path/to/getFaqById.graphql';
export const useFaqById = useLazyQuery(getFaqById);

Faq.jsx常见问题解答.jsx

const [fetchFaqById, {faqLoading, faqData}] = useFaqById;

const handleClick = (catId) => {
   fetchFaqById({
      variables: {
         id: catId
      }
   });
}

useEffect(() => {
   if (faqData) {
      setFaq([...faqData.faq]);
   }
}, [faqData])

You don't call the Query inside handleClick function directly you have to declare only inside the main function after that you have to use.您不要直接在 handleClick function 中调用查询,您必须只在主 function 中声明,之后您必须使用。

import { useLazyQuery } from '@apollo/react-hooks';
import getFaqById from 'path/to/getFaqById.graphql';
const [useFaqById,{data}] = useLazyQuery(getFaqById);

Then you can call inside the handleClick function然后就可以在handle里面调用Click function

const handleClick = (catId) => {
   useFaqById({
      variables: {
         id: catId
      }
   });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM