【发布时间】:2019-10-22 18:53:10
【问题描述】:
这是我的代码
import React, { FC, useState,useMemo } from "react";
const FormSearch: FC<{ params: string }> = ({
params
}) => {
const [category, useCategory] = useState<string>("All");
const [search, setSearch] = useState<string>("");
useMemo(() => {
const searchParams = new URLSearchParams(params);
if (!!searchParams.get("Categories")&&!!searchParams.get("Search")) {
useCategory(`${searchParams.get("Categories")}`);
setSearch(`${searchParams.get("Search")}`);
} else {
setSearch("");
useCategory("All")
}
}, [params]);
return (
<div>1000</div>
);
};
我收到这些错误
第 16 行:不能在回调中调用 React Hook “useCategory”。 React Hooks 必须在 React 函数组件或自定义 React Hook 函数中调用 react-hooks/rules-of-hooks 第 20 行:不能在回调中调用 React Hook “useCategory”。 React Hooks 必须在 React 函数组件或自定义 React Hook 函数 react-hooks/rules-of-hooks 中调用
【问题讨论】:
-
只能从 React 函数调用 React 钩子,无论是 React 函数组件还是自定义钩子。
-
@ Ulug Toprak 我违反了“不要在循环、条件或嵌套函数中调用钩子”的规则。 ??
-
@Ulug Toprak 我刚才写错了 useCategory 它是必要的 setCategory
标签: reactjs typescript