How to get the search param using useSearch() at the first time?
Hi all I am using the useSearch() hook to get the search param it returns the value whenever the search param changes. I want to only get the values at the component mount not every change how to do that?
3 Replies
conscious-sapphireOP•2y ago
const useSearchFirstTime = () => {
const [searchParam, setSearchParams] = useState<string | null | undefined>(
undefined,
);
const { search } = useSearch({
from: "/private-layout/home",
});
useEffect(() => {
setSearchParams(search);
}, []);
return searchParam;
};
I created the custom simple hook it works but I don't want useEffect and useState. Is there any alternative are there?
genetic-orange•2y ago
Put it in a ref
firstSearch wont change.
Keep in mind that the component will still rerender.
conscious-sapphireOP•2y ago
Thanks @Blume I will try this.