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 shuffleItems = (items) => { | |
return items.map(item => ({ sort: Math.random(), value: item })) | |
.sort((item1, item2) => item1.sort - item2.sort) | |
.map(item => item.value); | |
} | |
shuffleItems([1,2,3,4,5,6]); | |
shuffleItems([1,2,3,4,5,6,6,8,10,12]); |
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
function fetch(url, options) { | |
// create a new Promise object | |
return new Promise((resolve, reject) => { | |
// create a new XMLHttpRequest object | |
const xhr = new XMLHttpRequest(); | |
// handle the response from the server | |
xhr.onload = () => { | |
const response = new Response(xhr.responseText, { | |
status: xhr.status, |
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 { useState } from "react" | |
import useEventListener from "./useEventListener" | |
export default function useOnlineStatus() { | |
const [online, setOnline] = useState(navigator.onLine) | |
useEventListener("online", () => setOnline(navigator.onLine)) | |
useEventListener("offline", () => setOnline(navigator.onLine)) | |
return online |
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 { useState, useEffect } from "react" | |
export default function useSize(ref) { | |
const [size, setSize] = useState({}) | |
useEffect(() => { | |
if (ref.current == null) return | |
const observer = new ResizeObserver(([entry]) => setSize(entry.contentRect)) | |
observer.observe(ref.current) | |
return () => observer.disconnect() |
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 { useState, useCallback } from "react" | |
export default function useStateWithValidation(validationFunc, initialValue) { | |
const [state, setState] = useState(initialValue) | |
const [isValid, setIsValid] = useState(() => validationFunc(state)) | |
const onChange = useCallback( | |
nextState => { | |
const value = | |
typeof nextState === "function" ? nextState(state) : nextState |
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 { useState, useEffect } from "react" | |
export default function useGeolocation(options) { | |
const [loading, setLoading] = useState(true) | |
const [error, setError] = useState() | |
const [data, setData] = useState({}) | |
useEffect(() => { | |
const successHandler = e => { | |
setLoading(false) |
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 { useState } from "react" | |
import useEventListener from "./useEventListener" | |
export default function useWindowSize() { | |
const [windowSize, setWindowSize] = useState({ | |
width: window.innerWidth, | |
height: window.innerHeight, | |
}) | |
useEventListener("resize", () => { |
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, useState } from "react" | |
export default function useOnScreen(ref, rootMargin = "0px") { | |
const [isVisible, setIsVisible] = useState(false) | |
useEffect(() => { | |
if (ref.current == null) return | |
const observer = new IntersectionObserver( | |
([entry]) => setIsVisible(entry.isIntersecting), | |
{ rootMargin } |
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" | |
export default function useEventListener( | |
eventType, | |
callback, | |
element = window | |
) { | |
const callbackRef = useRef(callback) | |
useEffect(() => { |
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 useAsync from "./useAsync" | |
export default function useScript(url) { | |
return useAsync(() => { | |
const script = document.createElement("script") | |
script.src = url | |
script.async = true | |
return new Promise((resolve, reject) => { | |
script.addEventListener("load", resolve) |
NewerOlder