React Hook "useCategory" cannot be called inside a callback?

Here is my code

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 (
the <div>1000</div>
);
};


I get these errors

Line 16: React Hook "useCategory" may not be called inside the callback. React Hooks should be called in the component, function or React in the custom function Hook React response-hooks / rules-of-hooks Line 20: React Hook "useCategory" cannot be called inside the callback. React Hooks should be invoked in the component functions React or React custom function Hook to react to the clues / rules-leads
March 23rd 20 at 19:30
2 answers
March 23rd 20 at 19:32
Solution
We are in the error about the hooks. You have a handler to change the category begins with use...
rename it setCategory
March 23rd 20 at 19:34
I'm breaking the rules "don't call hooks inside loops, conditions or nested functions." ?? - Marcelle_Kozey10 commented on March 23rd 20 at 19:37
@Marcelle_Kozey10,
Call hooks only at the top level
. There is described a exception, but I guess while at it. - Gerson_Feil38 commented on March 23rd 20 at 19:40
@Gerson_Feil38, But this is a big minus to react hooks .And how to rewrite my cat. Thanks for the reply. - Marcelle_Kozey10 commented on March 23rd 20 at 19:43
@Marcelle_Kozey10, You words, write what you want, and that can be Your code and does not reflect reality(those that You really want).
Better code to do here is an example of React+TS , for example, then you can immediately edit it... - Gerson_Feil38 commented on March 23rd 20 at 19:46
The rule applies to the hooks. useCategory is not a hook, its just named wrong, Linter sees functions starting with use as hooks - beulah_Ebert27 commented on March 23rd 20 at 19:49
@beulah_Ebert27, bright eyes )) - Gerson_Feil38 commented on March 23rd 20 at 19:52
@Gerson_Feil38 and how you think. Will I have problem if I use the hooks inside the if.and if you can't write a hook inside an If How can you rewrite that such code? - Marcelle_Kozey10 commented on March 23rd 20 at 19:55
@Marcelle_Kozey10, I would, in General, somehow so made an example of - Gerson_Feil38 commented on March 23rd 20 at 19:58
@Gerson_Feil38 I presented a simple example can be done in principle but not always possible. Thanks for the good discussion and good luck. - Marcelle_Kozey10 commented on March 23rd 20 at 20:01
@Marcelle_Kozey10, therefore, in my opinion, the question should ask like this:
1. How to do something?
2. I decided to(try to resolve) this way, but maybe there is a better way?
As the complexity and the questions will be set. - Gerson_Feil38 commented on March 23rd 20 at 20:04

Find more questions by tags ReactTypeScript