Skip to content

Instantly share code, notes, and snippets.

@reecelucas
reecelucas / fetchGraphQL.js
Last active April 29, 2020 09:52
Simple util for querying a GraphQL service using the fetch API
const fetchGraphQL = async ({ url, query, variables, headers }) => {
const response = await fetch(url, {
method: "POST",
headers: {
...headers,
"Content-Type": "application/json"
},
body: JSON.stringify({ query, variables })
});
@reecelucas
reecelucas / featureConfig.js
Last active March 28, 2020 15:15
Basic feature flag implementation
export default {
FEATURE_A: {
name: 'FEATURE_A',
description: 'Human readable description of feature A',
enabled: {
development: true,
production: false
}
},
FEATURE_B: {
@reecelucas
reecelucas / FileInput.tsx
Created July 11, 2019 11:19
Custom React file select component
import * as React from 'react';
interface Props extends React.HTMLProps<HTMLInputElement> {
onFileSelect: (file: File) => void;
}
const FileInput = ({ onFileSelect, ...props }: Props) => {
const inputRef = React.useRef<HTMLInputElement>();
const onChange = (event: React.ChangeEvent) => {
@reecelucas
reecelucas / .babelrc
Created June 3, 2019 08:00
Webpack Config Starter
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage"
}
],
"@babel/preset-react"
],
@reecelucas
reecelucas / useWebSocket.js
Last active April 17, 2020 09:05
React hook to provide a declarative wrapper around the WebSockets API
import { useEffect, useRef } from 'react'
const isFunction = fn => typeof fn === 'function';
const connectionIsOpen = ws => ws && ws.readyState === WebSocket.OPEN;
/**
* Usage:
* const { webSocket, sendMessage, closeConnection } = useWebSocket({
* url: 'wss://echo.websocket.org/',
* onOpen: useCallback(event => {
@reecelucas
reecelucas / useProximity.js
Last active April 18, 2019 12:21
React hook to detect proximity to an element. https://codesandbox.io/embed/0qwjq74w0p.
import { useEffect, useState } from "react";
const useProximity = (elemRef, perimeter = 100) => {
const [withinPerimeter, setWithinPerimeter] = useState(false);
useEffect(() => {
if (!elemRef || !elemRef.current) return;
let ticking = false;
const { current } = elemRef;
@reecelucas
reecelucas / useScrollDirection.js
Last active September 8, 2021 20:07
React hook to detect scroll direction, based on the API of https://github.com/dollarshaveclub/scrolldir
const SCROLL_UP = "up";
const SCROLL_DOWN = "down";
const useScrollDirection = ({
initialDirection,
thresholdPixels,
off
} = {}) => {
const [scrollDir, setScrollDir] = useState(initialDirection);
@reecelucas
reecelucas / useScrollBlock.js
Last active April 5, 2024 09:12
React hook to enable/disable page scroll
import { useRef } from 'react';
const safeDocument = typeof document !== 'undefined' ? document : {};
/**
* Usage:
* const [blockScroll, allowScroll] = useScrollBlock();
*/
export default () => {
const scrollBlocked = useRef();
@reecelucas
reecelucas / useClickOutside.js
Last active April 2, 2019 16:32
React hook to detect when a click happens outside of a specified element
import { useEffect } from 'react';
/**
* Usage:
* useClickOutside(myRef, event => {
* console.log(`${event.target} is outside of ${myRef.current}`);
* });
*/
export default (elemRef, fn) => {
useEffect(() => {
@reecelucas
reecelucas / usePrevious.js
Last active March 30, 2020 18:06
React hook to get a previous prop/state value
import { useEffect, useRef } from 'react';
/**
* Usage:
* const [count, setCount] = useState(0);
* const prevCount = usePrevious(count);
*/
export default value => {
const ref = useRef();