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
useLayoutEffect(() => { | |
myRef.current.style.top = '100px' | |
}, []); |
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
// 阿喔😥 畫面會閃一下 | |
function Demo() { | |
const myRef = React.useRef(null); | |
useEffect(() => { | |
myRef.current.style.top = '100px' | |
}, []); | |
return ( | |
<div style={{position: 'relative'}}> |
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
// 即使有執行耗時的運算,這樣寫依舊不會拖到新畫面的出現速度 | |
function Demo() { | |
useEffect(() => { | |
doSomeExpensiveCoputation(param) | |
}, [param]) | |
// return jsx | |
} |
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
async function foo() { | |
let result = await asyncTask(); | |
console.log(result); | |
} | |
foo(); |
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
function *foo() { | |
let result = yield asyncTask(); | |
console.log(result); | |
} | |
runner(foo); // 強大的函式 |
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
function *foo() { | |
let result = yield asyncTask(); | |
console.log(result); | |
} | |
const it = foo(); | |
const p = it.next().value; | |
p.then( | |
(data) => { |
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
function *foo() { | |
let y = yield 'Send me a value'; | |
console.log('y is ', y); | |
} | |
const it = foo(); | |
it.next(); // 傳回的物件:{value: 'Send me a value', done: false} | |
it.next(5); // 印出:y is 5 | |
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
function *foo() { | |
yield 'half'; | |
yield 'done'; | |
} | |
const it = foo(); | |
it.next().value; // 'half' | |
it.next().value; // 'done' |
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
new Promise(resolve => { | |
resolve(1); | |
Promise.resolve() | |
.then(() => { | |
console.log(2); | |
return 'A'; | |
}) | |
.then(data => | |
console.log(data) | |
); |
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
function timeoutPromise(time) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
reject('Timeout!') | |
}, time); | |
}); | |
} | |
//假設foo會回傳一個promise | |
Promise.race([ |
NewerOlder