Skip to content

Instantly share code, notes, and snippets.

View jwrigh26's full-sized avatar

Justin Wright jwrigh26

View GitHub Profile
@jwrigh26
jwrigh26 / useScrollListener
Created September 24, 2019 17:57
React custom hook for listening to window scroll event
import {useEffect} from 'react';
export default function useScrollListener(callback = () => {}, ms = 150) {
let onFinishedId;
let scrollStarted = false;
let scrollDirection = 'down';
let scrollTop = 0;
let prevScrollTop = scrollTop;
useEffect(() => {
@jwrigh26
jwrigh26 / useLongPress
Created September 24, 2019 17:56
React gist for a custom hook: useLongPress
import {useState, useEffect, useCallback} from 'react';
export function useLongPress(callback = () => {}, ms = 400) {
const [startLongPress, setStartLongPress] = useState(false);
useEffect(() => {
let timerId;
if (startLongPress) {
timerId = setTimeout(callback, ms);
} else {
@jwrigh26
jwrigh26 / useReducer
Last active May 3, 2019 23:08
useReducer in a custom hook example
import {useReducer} from 'react';
export const rowAction = {
add: 'ACTION_ADD',
approve: 'ACTION_APPROVE',
edit: 'ACTION_EDIT',
note: 'ACTION_NOTE',
reset: 'ACTION_RESET',
};
@jwrigh26
jwrigh26 / TimerWithGCD.md
Last active September 5, 2016 19:29
Creating a timer with Grand Central Dispatch

##Creating a timer with Grand Central Dispatch

At the following is the implementation file of a sample class that shows, how to make a timer with the help of Grand Central Dispatch. The timer fires on a global queue, just change the queue to the main queue or any custom queue and the timer fires on this queue and not on the global queue anymore.

This was modified for swift 2.2 Also created an example of a background global queue variable. As well as a callback in the start timer so you can do some cool stuff everytime the timer is called.