Last active
April 5, 2018 13:33
-
-
Save awto/cefa330b45518d33508dfb8cf8d28911 to your computer and use it in GitHub Desktop.
Functional Reactive Component
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
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