Original idea: babel-plugin-jsx-control-statements
pnpm (recommended):
pnpm i -D swc-plugin-jsx-control-statementsor yarn
yarn add -D swc-plugin-jsx-control-statementsIn your SWC config, you have to add to jsc.experimental.plugins - ['swc-plugin-jsx-control-statements', {}], like in the following code:
jsc: {
experimental: {
plugins: [
['swc-plugin-jsx-control-statements', {}],
],
},
},import React from 'react';
const Greeting = () => {
const [closed, setClosed] = useState(false);
return (
<>
<If condition={!closed}>
Hello,
</If>
World
<If condition={!closed}>
<button onClick={() => setClosed(true)}>Close</button>
</If>
</>
)
};import React from 'react';
const Greeting = () => {
const [closed, setClosed] = useState(false);
return (
<>
<Choose>
<When condition={!closed}>
Hello,
</When>
<Otherwise>
Bye,
</Otherwise>
</Choose>
World
<If condition={!closed}>
<button onClick={() => setClosed(true)}>Close</button>
</If>
</>
)
};Define <For> like so:
// you must provide the key attribute yourself
<For each="item" of={ this.props.items }>
<span key={ item.id }>{ item.title }</span>
</For>
// using the index as key attribute is not stable if the array changes
<For each="item" index="idx" of={ [1,2,3] }>
<span key={ idx }>{ item }</span>
<span key={ idx + '_2' }>Static Text</span>
</For>Used to assign values to local variables:
// simple
<With foo={ 47 } bar={ 'test' }>
<span>{ foo }</span>
<span>{ bar }</span>
</With>
// nested
<With foo={ 47 }>
<With bar={ 'test' }>
<span>{ foo }</span>
<span>{ bar }</span>
</With>
</With>