Skip to content

Instantly share code, notes, and snippets.

View tjunghans's full-sized avatar
💭
Open for business

Thomas Junghans tjunghans

💭
Open for business
View GitHub Profile
@btoo
btoo / usePrevious.ts
Last active April 27, 2025 07:13
typescript type-safe version of usePrevious taken directly from the react docs https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state
import { useRef, useEffect } from 'react';
/**
* a type-safe version of the `usePrevious` hook described here:
* @see {@link https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state}
*/
export function usePrevious<T>(
value: T,
): ReturnType<typeof useRef<T>>['current'] {
const ref = useRef<T>();
@JannisRex
JannisRex / ErrorBoundary.test.native.js
Created April 1, 2020 07:05
Jest Test for <ErrorBoundary />
import React from 'react'
import { Text, View } from 'react-native'
import renderer from 'react-test-renderer'
import { ErrorBoundary } from '../../src/components/index'
describe('ErrorBoundary', () => {
let consoleErrorSpy
let errorMock
let workingComponent
@sindresorhus
sindresorhus / esm-package.md
Last active May 8, 2025 09:12
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@sibelius
sibelius / mock-uuid.ts
Created August 30, 2021 16:06
mock uuid v4 unique and stable for tests
jest.mock('uuid', () => {
const base = '9134e286-6f71-427a-bf00-';
let current = 100000000000;
return {
v4: () => {
const uuid = base + current.toString();
current++;
return uuid;