Created
November 27, 2018 05:21
-
-
Save gaku-sei/b724b4a3ec6ff10112d02355bdfae66a to your computer and use it in GitHub Desktop.
Simple Context.Provider coroutine
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
function coroutine<Props>( | |
generator: (props: Props) => IterableIterator<JSX.Element> | |
): React.ComponentType<Props> { | |
return (props: Props) => { | |
const iterator = generator(props); | |
const rec = (Component: JSX.Element, done: boolean): JSX.Element => { | |
if (!done) { | |
const { done, value } = iterator.next(); | |
return React.cloneElement(Component, { children: rec(value, done) }); | |
} | |
return Component; | |
}; | |
const { done, value } = iterator.next(); | |
return rec(value, done); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment