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 { getQuestion } from './readline.js' | |
const [question, close] = getQuestion() | |
let input = await question('Please input a number') | |
console.log(`The number is: ${input}.`) | |
close() |
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 { 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 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 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 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); | |
}); |