This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import useClickPrevention from "./use-click-prevention"; | |
const MyComponent = () => { | |
const onClick = () => console.log("single click"); | |
const onDoubleClick = () => console.log("double click"); | |
const [onClickWithPrevention, onDoubleClickWithPrevention] = useClickPrevention({ onClick, onDoubleClick }); | |
return ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const noop = () => {}; | |
const requestTimeout = (fn, delay, registerCancel) => { | |
const start = new Date().getTime(); | |
const loop = () => { | |
const delta = new Date().getTime() - start; | |
if (delta >= delay) { | |
fn(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const noop = () => {}; | |
const browserSupportsRaf = | |
window.requestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
(window.mozRequestAnimationFrame && window.mozCancelRequestAnimationFrame) || // Firefox 5 ships without cancel support | |
window.oRequestAnimationFrame || | |
window.msRequestAnimationFrame; | |
const requestTimeoutNoRaf = (fn, delay, registerCancel) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const noop = () => {}; | |
const requestTimeout = (fn, delay, registerCancel) => { | |
const start = new Date().getTime(); | |
const loop = () => { | |
const delta = new Date().getTime() - start; | |
if (delta >= delay) { | |
fn(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef } from "react"; | |
const noop = () => {}; | |
const useCancelableScheduledWork = () => { | |
const cancelCallback = useRef(noop); | |
const registerCancel = fn => (cancelCallback.current = fn); | |
const cancelScheduledWork = () => cancelCallback.current(); | |
// Cancels the current sheduled work before the "unmount" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const browserIsAnimationFrameIncompatible = | |
!window.requestAnimationFrame && | |
!window.webkitRequestAnimationFrame && | |
!window.mozRequestAnimationFrame && | |
!window.oRequestAnimationFrame && | |
!window.msRequestAnimationFrame; | |
const requestInterval = (fn, delay, registerCancel) => { | |
if (browserIsAnimationFrameIncompatible) { | |
const intervalId = window.setInterval(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect, useCallback } from "react"; | |
const Counter = ({ notify }) => { | |
const [value, setValue] = useState(0); | |
useEffect(() => { | |
notify(value); | |
}, [value, notify]); | |
return <button onClick={() => setValue(value + 1)}>Increment</button>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
const Counter = ({ notify }) => { | |
const [value, setValue] = useState(0); | |
useEffect(() => { | |
notify(value); | |
}, [value, notify]); | |
return <button onClick={() => setValue(value + 1)}>Increment</button>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
const Counter = ({ notify }) => { | |
const [value, setValue] = useState(0); | |
useEffect(() => { | |
notify(value); | |
}, [value]); | |
return <button onClick={() => setValue(value + 1)}>Increment</button>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
const Component = ({ notifyEven }) => { | |
const [value, setValue] = useState(0); | |
useEffect(() => { | |
if ((value % 2) === 0) { | |
notifyEven(value); | |
} | |
}, [value]); |
NewerOlder