简体   繁体   English

next.js中的hasura过滤总是一键延迟

[英]hasura filtering in next.js is always one key to late

I am getting data from hasura though useRecipe_Filter and passing searchFilter state for it as a variable.It looks like everytime i press a key ui updates one key press to late filtered data is passed to results state我通过 useRecipe_Filter 从 hasura 获取数据并将 searchFilter state 作为变量传递给它。看起来每次我按下一个键 ui 都会更新一个按键以延迟过滤的数据被传递到结果 state

const SearchBar = (props: any) => {

key values state used as a variable for useRecipe_filter键值 state 用作 useRecipe_filter 的变量

  const [searchFilter, setSearchFilter] = useState('');
  const { data, loading, error } = useRecipe_Filter({
    variables: {
      title: `%${searchFilter}%`
    }
  });
  const filterRecipes = (e: any) => {
    setSearchFilter(e.target.value);
    let mappedData: any = [];
    if (data && data.recipes) {
      if (loading) return <p>Loading...</p>;
      if (error) return <p>{error.message}</p>;
      mappedData = data.recipes.map((recipe: any) => {
        return {
          id: recipe.id,
          title: recipe.title,
          image: recipe.image,
          ingredients: recipe.ingredient_relation.map((ingredient: any) => {
            return {
              id: ingredient.id,
              name: ingredient.name
            };
          }),
          description: recipe.description
        };
      });
    }
    props.setResults(mappedData);
  };
  const handleAddNewRecipe = () => {
    props.onOpen();
    if (props.onSetChangeName(true) || props.onSetShowButton(true)) {
      props.onSetShowButton(false);
      props.onSetChangeName(false);
    }
    props.setFormName('');
    props.setFormImage('');
    props.setFormDescription('');
    props.setFormIngredient('');
  };

  return (
    <div>
      <Container>
        <input
          type="text"
          placeholder="Search for a recipe..."
          value={searchFilter}
          onChange={filterRecipes}
        />
        <button type="button" id="btn" onClick={handleAddNewRecipe}>
          Add new recipe
        </button>
      </Container>
      <RecipeList
        onOpen={props.onOpen}
        recipe={props.results}
        setRecipe={props.setResults}
        onSetShowButton={props.onSetShowButton}
        onSetChangeName={props.onSetChangeName}
        setFormName={props.setFormName}
        setFormImage={props.setFormImage}
        setFormDescription={props.setFormDescription}
        setFormIngredient={props.setFormIngredient}
      />
    </div>
  );
};

here is the query i used _ilike lets compare by case insensitive letters and i used %% to be able to compare by one letter这是我使用的查询 _ilike 让我们按不区分大小写的字母进行比较,我使用 %% 以便能够按一个字母进行比较

query recipe_filter($title: String!) {
  recipes(where: { title: { _ilike: $title } }) {
    id
    title
    image
    description
    ingredient_relation {
      id
      name
    }
  }
}

I solved this by adding useEffect for data that im geting from hasura like this我通过为像这样从 hasura 获取的数据添加 useEffect 来解决这个问题

  useEffect(() => {
    props.setResults(mappedData);
  }, [data]);

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

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