Skip to content

Instantly share code, notes, and snippets.

@awto
Last active April 5, 2018 13:33
Show Gist options
  • Save awto/cefa330b45518d33508dfb8cf8d28911 to your computer and use it in GitHub Desktop.
Save awto/cefa330b45518d33508dfb8cf8d28911 to your computer and use it in GitHub Desktop.
Functional Reactive Component
export function reactiveComponent(fun) {
class Reactive extends React.PureComponent {
constructor(props) {
super(props)
this.state = {current: null}
}
componentDidMount() {
(async () => {
for await(const current of fun(this.props))
this.setState({current})
})()
}
render() {
return this.state.current
}
}
Reactive.displayName = fun.name
return Reactive
}
const AsyncLoad = reactiveComponent(async function*(props){
yield <div>loading...</div>
yield await import(props.url)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment