0.4.0 • Published 30 days ago

swc-plugin-jsx-control-statements v0.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
30 days ago

🦀 JSX control statements for swc

Original idea: babel-plugin-jsx-control-statements

Installation

pnpm (recommended):

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

or yarn

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

Configure swc

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', {}],
        ],
    },
},

Usage

<If> tag

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)

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
0.3.0

30 days ago

0.4.0

30 days ago

0.2.0

10 months ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.0

1 year ago