【问题标题】:Apollo is it possible to clear the data obj after calling useLazyQueryApollo 是否可以在调用 useLazyQuery 后清除数据 obj
【发布时间】:2021-05-07 07:04:09
【问题描述】:

我为我创建的搜索栏调用了 apollo 惰性查询钩子,当我清除搜索栏中的数据时,我想从钩子中清除数据变量。我无法在线查看这是否可能

const [
    getAutoComplete,
    { data: autoCompleteItems, loading: loadingAutoComplete },
  ] = useLazyQuery(SEARCH, {
    variables: { limit: 10 },
  });

我在这里打电话给getAutoComplete

useEffect(() => {
    if (searchQuery.length > 1 && searchQuery !== '') {
      getAutoComplete({ variables: { criteria: searchQuery } });
    }

    if (searchFocus && searchQuery === '') 
      // In here I want to do something like set autoCompleteItems = undefined
      getAutoComplete({ variables: { criteria: '{}' } });
    }
  }, [searchQuery]);

基本上,当我清除 searchQuery 时,autoCompleteItems 变量仍在使用前一个。但是当它为空时,我希望它是未定义的,最好不要调用 api。

【问题讨论】:

    标签: reactjs graphql


    【解决方案1】:

    你什么时候清除 searchQuery,graphql 缓存你的数据,所以如果你不需要,像这样使用 no-cache:

        getAutoComplete,
        { data: autoCompleteItems, loading: loadingAutoComplete },
      ] = useLazyQuery(SEARCH, {
        variables: { limit: 10 },
        fetchPolicy: 'no-cache',
      });
    

    【讨论】:

    猜你喜欢
    • 2020-05-31
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2021-01-29
    • 2021-07-15
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多