T
TanStack2y ago
conscious-sapphire

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-sapphire
conscious-sapphireOP2y 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
genetic-orange2y ago
Put it in a ref
const { search } = useSearch(...);
const firstSearch = useRef(search);
const { search } = useSearch(...);
const firstSearch = useRef(search);
firstSearch wont change. Keep in mind that the component will still rerender.
conscious-sapphire
conscious-sapphireOP2y ago
Thanks @Blume I will try this.

Did you find this page helpful?