# swc-plugin-jsx-control-statements

> SWC plugin for jsx-control-statements

Latest version **0.7.1** (published 2025-02-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install swc-plugin-jsx-control-statements
pnpm add swc-plugin-jsx-control-statements
yarn add swc-plugin-jsx-control-statements
bun add swc-plugin-jsx-control-statements
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.1 |
| Published | 2025-02-03 |
| First published | 2023-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 708.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | intpp |
| Maintainers | intpp |
| Keywords | react, jsx, if, else, swc, swc-core, swc-plugin, jsx-control-statements, react-component |

## Links

- npm: https://www.npmjs.com/package/swc-plugin-jsx-control-statements
- Repository: https://github.com/intpp/swc-plugin-jsx-control-statements
- Homepage: https://github.com/intpp/swc-plugin-jsx-control-statements#readme
- Issues: https://github.com/intpp/swc-plugin-jsx-control-statements/issues
- npm.io page: https://npm.io/package/swc-plugin-jsx-control-statements

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.7.1 (latest) — 2025-02-03
- 0.6.0 — 2024-10-03
- 0.5.0 — 2024-07-23
- 0.4.0 — 2024-04-10
- 0.3.0 — 2024-04-10
- 0.2.0 — 2023-07-11
- 0.1.3 — 2023-04-05
- 0.1.2 — 2023-04-05
- 0.1.1 — 2023-04-04
- 0.1.0 — 2023-04-04
- 0.0.0 — 2023-04-03

## README

# 🦀 JSX control statements for swc

Original idea: [babel-plugin-jsx-control-statements](https://github.com/AlexGilleran/jsx-control-statements)

## Installation

**pnpm** (recommended):

```shell
pnpm i -D swc-plugin-jsx-control-statements
```

or **yarn**

```shell
yarn add -D swc-plugin-jsx-control-statements
```

## Configure `swc`

In your SWC config, you have to add to [`jsc.experimental.plugins`](https://swc.rs/docs/configuration/compilation#jscexperimentalplugins) - `['swc-plugin-jsx-control-statements', {}]`, like in the following code:

```javascript
jsc: {
    experimental: {
        plugins: [
            ['swc-plugin-jsx-control-statements', {}],
        ],
    },
},
```

## Usage

### `<If>` tag

```jsx
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>
        </>
    )
};
```

### `<Choose>` tag (if - else if - else condition)

```jsx
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>
        </>
    )
};
```

### `For` tag (like array map function)

```jsx
import React from 'react';

const TodoList = ({ items }) => {
    return (
        <For each="item" of={items}>
            <span key={item.id}>{item.title}</span>
        </For>
    );
};
```

> [!WARNING]  
> using the index as key attribute is not stable if the array changes
>
> ```tsx
> <For each="item" index="idx" of={[1, 2, 3]}>
>   <span key={idx}>{item}</span>
>   <span key={`${idx}_2`}>Static Text</span>
> </For>
>```

### `With` tag

#### Simple

```jsx
import React from 'react';

const SomeComponent = ({ items }) => {
    return (
        <With foo={47} bar="test">
            <span>{foo}</span>
            <span>{bar}</span>
        </With>
    );
};
```

#### Nested

```jsx
import React from 'react';

const SomeComponent = ({ items }) => {
    return (
        <With foo={47}>
            <With bar="test">
                <span>{foo}</span>
                <span>{bar}</span>
            </With>
        </With>
    );
};
```

---
_Source: https://npm.io/package/swc-plugin-jsx-control-statements · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
