This file contains hidden or 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 readline from 'node:readline' | |
| async function ask(query) { | |
| const rl = readline.createInterface({ input: process.stdin, output: process.stdout, tabSize: 4 }); | |
| return new Promise((resolve) => rl.question(query, (answer) => { | |
| rl.close(); | |
| resolve(answer); | |
| })); | |
| } |
This file contains hidden or 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 { createContext, useState, useContext } from 'react' | |
| // Create the context in a separate file. Import it to every component that needs it. | |
| const CounterContext = createContext(0) | |
| const ContextDemo = () => { | |
| const [count, setCount] = useState(2) | |
| const contextValue = { count, setCount } |
This file contains hidden or 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 { useRef } from 'react' | |
| const MediaDevice = () => { | |
| const videoRef = useRef(null) | |
| const handleVideoOn = () => assignStream(videoRef.current) | |
| const handleVideoOff = () => videoRef.current.srcObject = null | |
| return ( | |
| <div> |
This file contains hidden or 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' | |
| /* | |
| Safely send and cancel one AJAX request at the time. If you need to send multiple requests at the same time, call this hook several times. | |
| Usage: | |
| const [cancelRef, doAjax] = useCancellableAjax() | |
| Fetch data. Tip: use a setState function as callback. | |
| doAjax(url, fetchOptions, dataCallback) |
This file contains hidden or 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
| // Copy and paste this code into your main script file | |
| // The purpose is to register the service worker | |
| // The browser will load sw.js for us if you do this | |
| if ('serviceWorker' in navigator) { | |
| // Assumes your service worker has file name "sw.js" | |
| navigator.serviceWorker.register('sw.js') | |
| .then(reg => { | |
| console.log('Registration succeeded. Scope is ' + reg.scope); | |
| }); |