#How to get the search param using useSearch() at the first time?

3 messages · Page 1 of 1 (latest)

olive vessel
#

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?

olive vessel
#

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?

valid cave
#

Put it in a ref

const { search } = useSearch(...);
const firstSearch = useRef(search);

firstSearch wont change.
Keep in mind that the component will still rerender.