Skip to content

Instantly share code, notes, and snippets.

View cosemansp's full-sized avatar

Peter Cosemans cosemansp

  • Euricom
  • Belgium
View GitHub Profile
/* eslint-disable no-underscore-dangle */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useMutation, UseMutationResult, useQuery, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
type BaseFn = (...args: any[]) => unknown;
type FirstParameter<TFn extends BaseFn> = Parameters<TFn>[0] extends undefined ? void : Parameters<TFn>[0];
type Procedure<TFn = BaseFn> = {
/**
* @deprecated - internal use only
variables:
pnpm_config_cache: $(Pipeline.Workspace)/.pnpm-store
steps:
- task: Cache@2
inputs:
key: 'pnpm | "$(Agent.OS)" | pnpm-lock.yaml'
path: $(pnpm_config_cache)
displayName: Cache pnpm
variables:
pnpm_config_cache: $(Pipeline.Workspace)/.pnpm-store
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
variables:
pnpm_config_cache: $(Pipeline.Workspace)/.pnpm-store
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
import * as React from 'react';
// To avoiding Use Effect Getting Called Twice
// See https://dev.to/ag-grid/react-18-avoiding-use-effect-getting-called-twice-4i9e
export const useMount = (effect: () => void | (() => void)) => {
const destroyFunc = React.useRef<void | (() => void)>();
const effectCalled = React.useRef(false);
const renderAfterCalled = React.useRef(false);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [val, setVal] = React.useState<number>(0);
import * as React from 'react';
import useApi from './useApi';
import { useMount } from './useMount';
type MutationOptions<TData, TError> = {
onError?: (err: TError, config: MutationConfig) => void;
onSuccess?: (data: TData, config: MutationConfig) => void;
};
type MutationConfig = {
/* eslint-disable @typescript-eslint/no-explicit-any */
import { waitFor, renderHook } from '@testing-library/react';
import { AxiosError } from 'axios';
import nock from 'nock';
import useQuery from './useQuery';
// mock useAuth
vi.mock('./useAuth', () => ({
default: vi.fn(() => ({
isAuthenticated: true,
function createQueryFn(baseUrl: string): QueryFunction {
return async ({queryKey}) => {
const path =
typeof queryKey === 'string' ? queryKey : queryKey[0] + qs(queryKey[1])
const res = await fetch(baseUrl + path)
if (!res.ok) throw new Error(await res.json())
return res.json()
}
function useDebounce(value, delay) {
// State and setters for debounced value
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(
() => {
// Update debounced value after delay
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
// Cancel the timeout if value changes (also on delay change or unmount)
@cosemansp
cosemansp / gist:ec3aad99cfa506e02c85058b9272926f
Created November 15, 2020 10:41
Improved React Context
import { createContext, useContext, useMemo, ConsumerProps, ReactNode } from 'react'
interface Value {
mode: 'dark' | 'light';
setMode: (mode: 'dark' | 'light') => void;
compact: boolean;
setCompact: (compact: boolean) => void;
}
const Context = createContext<Value | null>(null)