Original idea: babel-plugin-jsx-control-statements
pnpm (recommended):
pnpm i -D swc-plugin-jsx-control-statements
or yarn
yarn add -D swc-plugin-jsx-control-statements
In 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>
</>
)
};
TODO:
- [ ] Support
<For />
tag