Last active
April 18, 2020 15:48
-
-
Save undoZen/5ae1d2026e7f6d92ea1d3ff0cdb6984d to your computer and use it in GitHub Desktop.
use-rx-init-demo
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, { memo, Suspense, useMemo } from 'react'; | |
import { useRxInit, suspensify } from '@/utils'; | |
import { of, from, Observable } from 'rxjs'; | |
let r = (n, ms) => | |
new Promise(resolve => { | |
setTimeout(() => resolve(n), ms); | |
}); | |
let Loading = () => <span>loading...</span>; | |
let SayHello = memo(({ ctx }) => { | |
let b = typeof ctx.b === 'function' ? ctx.b() : ctx.b; | |
return ( | |
<p> | |
hello, {ctx.a()} and {b} | |
</p> | |
); | |
}); | |
let Page = memo(() => { | |
console.log('render'); | |
let ctx = useRxInit(() => { | |
let a = suspensify(from(r(1, 1000)) as Observable<number>); | |
return { | |
a, | |
b: of(2), | |
}; | |
}); | |
console.log('ab', Date.now(), ctx); | |
ctx.a; | |
let ctx2 = useRxInit(() => { | |
let a = suspensify(from(r(2, 2000)) as Observable<number>); | |
let b = suspensify(from(r(3, 3000)) as Observable<number>); | |
return { a, b }; | |
}); | |
return ( | |
<> | |
<Suspense fallback={<Loading />}> | |
<SayHello ctx={ctx} /> | |
</Suspense> | |
<Suspense fallback={<Loading />}> | |
<SayHello ctx={ctx2} /> | |
</Suspense> | |
</> | |
); | |
}); | |
export default Page; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment