Skip to content

Instantly share code, notes, and snippets.

View andycarrell's full-sized avatar

Andy Carrell andycarrell

View GitHub Profile
function useHTMLContainer() {
const container = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const element = document.createElement('div');
document.body.appendChild(element);
container.current = element;
return () => {
if (container.current) {
/**
* @link https://raw.githubusercontent.com/NaturalCycles/js-lib/master/src/promise/pProps.ts
* Promise.all for Object instead of Array.
*
* Inspired by Bluebird Promise.props() and https://github.com/sindresorhus/p-props
*
* Improvements:
*
* - Exported as { promiseProps }, so IDE auto-completion works
* - Simpler: no support for Map, Mapper, Options
const { rename, mkdir, rmdir } = require("fs");
const copyfiles = require("copyfiles");
const { promisify } = require("util");
const temporaryDirectory = "./nzpqofqtzj";
const mkdirAsync = promisify(mkdir);
const rmdirAsync = promisify(rmdir);
const renameAsync = promisify(rename);
const copyfilesAsync = promisify(copyfiles);
import { useCallback, useLayoutEffect, useRef } from "react";
/**
* Returns a constant reference to a callback which can be safely added
* to dependency arrays without triggering the effect or memoization.
* Maintains an up to date copy of the provided function to avoid
* referencing stale state/props at calltime.
*/
const useEventCallback(fn) {
const ref = useRef(fn);
import { useEffect, useState } from "react";
const useScrollPosition = (callback) => {
// copy callback to ref, using useLayoutEffect
const callbackRef = useConstantRefCallback(callback);
useEffect(() => {
let lastKnownScrollPosition = 0;
let previousLastKnownScrollPosition = 0;
let ticking = false;

Keybase proof

I hereby claim:

  • I am andycarrell on github.
  • I am andycarrell (https://keybase.io/andycarrell) on keybase.
  • I have a public key ASCiGobDmIpg0aopxQ7f0VOS4D1p5vnDvBus02k6KthChAo

To claim this, I am signing this object:

import { useCallback } from "react";
import { useApolloClient } from "@apollo/client";
import { defaultDataIdFromObject } from "@apollo/client/core";
const useApolloCacheEvict = () => {
const { cache } = useApolloClient();
/**
* Pass entire objects as queried from the cache.
* Use Apollo's default function to determine id.
function useRefGetAndSet(value) {
const ref = React.useRef(value);
const getRef = React.useCallback(() => ref.current, []);
const setRef = React.useCallback((newValue) => {
ref.current = newValue;
}, []);
// Optional: sync value every render
useLayoutEffect(() => {
export async function getStaticProps({ params }) {
const { slug } = params;
const post = postFor[slug];
if (!post) {
return { props: {} };
}
const { default: Post, SEO } = await import(`../../posts/${post}`);
export default function useTimeout() {
const ids = useRef([]);
React.useEffect(() => {
const cleanupTimeouts = () => {
ids.current.forEach(clearTimeout);
};
return cleanupTimeouts;
}, []);