Skip to content

Instantly share code, notes, and snippets.

View tekno0ryder's full-sized avatar

Ahmed Alsinan tekno0ryder

View GitHub Profile
@tekno0ryder
tekno0ryder / useLastSearch.ts
Created May 3, 2023 16:31
A custom hook to save last query search in local storage
import { useEffect } from "react";
import { useLocation } from "react-router";
import { useNavigate } from "react-router-dom";
import { useLocalStorage } from "usehooks-ts";
// A hook to save last search query and load it again when `loadLastSearch` is received in location state
// Useful to save table state and load it when user click on custom in-page back button
export const useLastSearch = (key: string, isDisabled?: boolean) => {
@tekno0ryder
tekno0ryder / useQueryParam.tsx
Last active May 26, 2023 12:09
Custom hook to store state in query params
import { createContext, useContext, useEffect, useRef, useState } from "react";
import { useSearchParams } from "react-router-dom";
const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
const isFalsy = (value: any) =>
!value ||
JSON.stringify(value) === "{}" ||
(Array.isArray(value) && value.length === 0);