Open MurakamiKennzo opened 3 years ago
and this?
import { Observable, Subject } from "rxjs";
import { map, startWith, pluck } from 'rxjs/operators'
import React, { useState, useEffect } from 'react'
import ReactDOM from 'react-dom'
type React$<T> = Observable<[T, React.ReactElement]>
const Input$ = ((): React$<string> => {
const event$ = new Subject<React.ChangeEvent<HTMLInputElement>>()
return event$.pipe(
pluck('target', 'value'),
startWith(''),
map(s => [
s,
<input value={s} onChange={e => event$.next(e)} />
])
)
})();
const App$: React$<string> = Input$.pipe(
map(([s, vdom]) => [
s,
<>
<p>the current value is: {s}</p>
{vdom}
</>
])
)
const runApp = <T extends {} = any>(element: HTMLElement | null, App$: React$<T>) => {
const App = () => {
const [vdom, setVdom] = useState<React.ReactElement | null>(null)
useEffect(() => {
const subscription = App$.pipe(map(x => x[1])).subscribe(setVdom)
return () => {
subscription.unsubscribe()
}
}, [])
return vdom
}
ReactDOM.render(<App />, element)
}
runApp(document.getElementById('app'), App$)
Here is another way when using rxjs in react:
Do this is a better way?