Skip to content

Instantly share code, notes, and snippets.

View Tim-W-James's full-sized avatar
🤓

TimJ Tim-W-James

🤓
View GitHub Profile
@Tim-W-James
Tim-W-James / ConditionalWrapper.tsx
Created October 17, 2023 01:35
React ConditionalWrapper
type ConditionalWrapperProps = {
/**
* Inner component to be wrapped.
*/
children: React.ReactElement;
/**
* Condition to be met for the wrapper to be applied.
*/
condition: boolean;
/**
@Tim-W-James
Tim-W-James / useOnlineStatus-hook.ts
Created February 12, 2023 07:04
React useOnlineStatus Custom Hook #hooks
const useOnlineStatus = () => {
const [online, setOnline] = useState(navigator.onLine);
useEventListener("online", () => setOnline(navigator.onLine));
useEventListener("offline", () => setOnline(navigator.onLine));
return online;
};
@Tim-W-James
Tim-W-James / usePrevious-hook.ts
Created February 12, 2023 06:58
React usePrevious Custom Hook #hooks
const usePrevious = <T>(value: T): T => {
// The ref object is a generic container whose current property is mutable ...
// ... and can hold any value, similar to an instance property on a class
const ref: any = useRef<T>();
// Store current value in ref
useEffect(() => {
ref.current = value;
}, [value]); // Only re-run if value changes
@Tim-W-James
Tim-W-James / useLockBodyScroll-hook.ts
Created February 12, 2023 06:52
React useLockBodyScroll Custom Hook #hooks
const useLockBodyScroll = (): void => {
// useLaoutEffect callback return type is "() => void" type
useLayoutEffect((): (() => void) => {
// Get original body overflow
const originalStyle: string = window.getComputedStyle(
document.body
).overflow;
// Prevent scrolling on mount
document.body.style.overflow = "hidden";
// Re-enable scrolling when component unmounts
@Tim-W-James
Tim-W-James / useIntersectionObserver-hook.ts
Created February 12, 2023 06:37
React useIntersectionObserver Custom Hook #hooks
import { RefObject, useEffect, useState } from "react";
const useIntersectionObserver = (
ref: RefObject<Element>,
rootMargin = "0px"
): boolean => {
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
const current = ref.current;
@Tim-W-James
Tim-W-James / lazy-load-named.ts
Last active February 12, 2023 05:31
React Lazy Load Named Export #react
/**
* Lazy load a module with a named export
*
* @param {string} path - path for the import
* @param {string} namedExport - named exported member of the module
*/
const lazyLoad = (path: string, namedExport: string) =>
lazy(() =>
namedExport
? import(path).then((module) => ({ default: module[namedExport] }))
@Tim-W-James
Tim-W-James / useLocalStorage-hook.tsx
Created February 12, 2023 04:50
React useLocalStorage Custom Hook #hooks
// Adapted from: https://usehooks.com/useLocalStorage/
/**
* Get and set a value in browser local storage
*
* @param {string} key - key for the data
* @param {T} initialValue - initial data value
*/
const useLocalStorage = <T>(key: string, initialValue: T) => {
// State to store our value
@Tim-W-James
Tim-W-James / useDocumentMeta-hook.tsx
Last active February 12, 2023 04:47
React useDocumentMeta Custom Hook #hooks
import { useEffect } from "react";
/**
* Set the document meta attributes
*
* @param {string} title - meta title
* @param {string} description - meta description
*/
const useDocumentMeta = (
title?: string,
@Tim-W-James
Tim-W-James / conditional-attribute.js
Last active February 12, 2023 03:21
JavaScript Spread Conditional Object Attribute
const x = {
...( condition && { key: value })
}
@Tim-W-James
Tim-W-James / urlSearchParams.js
Created October 4, 2022 09:02
Node Encode www-form-urlencoded
new URLSearchParams({
key: "value"
})