babel-plugin-jsx-fragment

better element arrays in jsx

MIT 9 个版本
安装
npm install babel-plugin-jsx-fragment
yarn add babel-plugin-jsx-fragment
pnpm add babel-plugin-jsx-fragment
bun add babel-plugin-jsx-fragment
README

jsx fragment babel plugin

npm i -S babel-plugin-jsx-fragment

To use include jsx-fragment in your plugins array in your .babelrc or config object. Works with React 0.13+ . By default the fragment tag name is <frag> but you can configure it to whatever you want with the tagName option.

{
  "plugins": [
    ["jsx-fragment", { "tagName": "fragment" }]
  ]
}

The Problem

JSX gets ugly when using conditionals that return more than one jsx element

<div>
{ true && [
    <span/>, <div/>
  ]
}
</div>

You need to use commas (gross) and an array literal (yuck). jsx-fragment allows for a simple syntax to hide the ugliness, based on the discussion here. Just use the pseudo element <frag> to wrap arrays of Elements.

<div>
{ condition && <frag>
    <span/>
    <div/>
  </frag>
}
</div>

the <frag> element will be compiled away into the following.

React.createElement("div", null, condition && ReactFragment.create({
    key_0: React.createElement("span", null),
    key_1: React.createElement("div", null)
  })
);
版本列表
4.0.3 2017-04-25
4.0.2 2016-11-11
4.0.1 2016-11-10
4.0.0 2016-11-10
3.0.1 2016-08-13
3.0.0 2016-08-13
2.0.0 2015-11-18
1.1.0 2015-06-05
1.0.0 2015-06-05