switch
is a jsx plugin.
It helps to select the first children that does not return undefined
.
npm
npm i @innet/switch
yarn
yarn add @innet/switch
Use it if you do not want to handle promises.
import innet, { createHandler } from 'innet'
import { switchSync } from '@innet/switch'
import { jsxPlugins } from '@innet/jsx'
import { object } from '@innet/utils'
const handler = createHandler([
object([
jsxPlugins({
switch: switchSync,
}),
]),
])
const app = (
<switch>
{undefined}
test1
{'test2'}
</switch>
)
innet(app, handler)
// 'test1'
Use it when you want to handle promises.
It waits while a promise ends and the continues the checking.
import innet, { createHandler } from 'innet'
import { switchAsync } from '@innet/switch'
import { jsxPlugins } from '@innet/jsx'
import { object } from '@innet/utils'
const handler = createHandler([
object([
jsxPlugins({
switch: switchAsync,
}),
]),
])
const app = (
<switch>
{undefined}
{new Promise(resolve => resolve('test1'))}
{'test2'}
</switch>
)
await innet(app, handler)
// 'test1'
Change resolve('test1')
to resolve(undefined)
and you get test2
If you find a bug or have a suggestion, please file an issue on GitHub.