Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/AlexGilleran/jsx-control-statements
Neater If and For for React JSX
https://github.com/AlexGilleran/jsx-control-statements
babel babel-plugin javascript jsx react
Last synced: about 2 months ago
JSON representation
Neater If and For for React JSX
- Host: GitHub
- URL: https://github.com/AlexGilleran/jsx-control-statements
- Owner: AlexGilleran
- License: mit
- Created: 2015-02-19T03:03:44.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T16:15:45.000Z (almost 2 years ago)
- Last Synced: 2024-10-26T09:29:21.665Z (about 2 months ago)
- Topics: babel, babel-plugin, javascript, jsx, react
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/babel-plugin-jsx-control-statements
- Size: 693 KB
- Stars: 1,618
- Watchers: 26
- Forks: 64
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-react-components-all - jsx-control-statements - Neater If and For for React JSX. (Uncategorized / Uncategorized)
- awesome-react-components - jsx-control-statements - Neater If and For for React JSX. (Code Design / HTML Template)
- awesome-github-star - jsx-control-statements
- awesome-list - jsx-control-statements - Neater If and For for React JSX. (Code Design / HTML Template)
- awesome-react-components - jsx-control-statements - Neater If and For for React JSX. (Code Design / HTML Template)
- awesome-react-components - jsx-control-statements - Neater If and For for React JSX. (Code Design / HTML Template)
- awesome-react-components - jsx-control-statements - Neater If and For for React JSX. (Code Design / HTML Template)
- fucking-awesome-react-components - jsx-control-statements - Neater If and For for React JSX. (Code Design / HTML Template)
README
# JSX Control Statements
[![Build Status](https://travis-ci.org/AlexGilleran/jsx-control-statements.svg?branch=master)](https://travis-ci.org/AlexGilleran/jsx-control-statements) [![Coverage Status](https://coveralls.io/repos/AlexGilleran/jsx-control-statements/badge.svg?branch=master&service=github)](https://coveralls.io/github/AlexGilleran/jsx-control-statements?branch=master) [![npm version](https://img.shields.io/npm/v/babel-plugin-jsx-control-statements.svg?style=flat)](https://www.npmjs.com/package/babel-plugin-jsx-control-statements)
_JSX-Control-Statements_ is a Babel plugin that extends JSX to add basic control statements: **conditionals** and **loops**.
It does so by transforming component-like control statements to their JavaScript counterparts - e.g. `Hello World!` becomes `condition() ? 'Hello World!' : null`.Developers coming to React from using JavaScript templating libraries like Handlebars are often surprised that there's no built-in looping or conditional syntax. This is by design - JSX is not a templating library, it's declarative syntactic sugar over functional JavaScript expressions. JSX Control Statements follows the same principle - it provides a component-like syntax that keeps your `render` functions neat and readable, but desugars into clean, readable JavaScript.
The only dependency _JSX-Control-Statements_ relies upon is _Babel_. It is compatible with React and React Native.
:skull_and_crossbones: Beware: This is a Babel plugin. It changes your code to other code - this means that some tooling that looks at your code (e.g. static analysis, typescript) is likely to not work. This plugin dates back to when JSX was daring and Javascript was more like playdough than meccano - if you want to stay on the well-trodden path stick with writing `&&` and `map`.
## Table of Contents
- [A Note on Transformation and Alternative Solutions](#a-note-on-transformation-and-alternative-solutions)
- [Installation](#installation)
- [Syntax](#syntax)
- [If Tag](#if-tag)
- [``](#if)
- [`` (deprecated)](#else)
- [Transformation](#transformation-1)
- [Choose Tag](#choose-tag)
- [``](#choose)
- [``](#when)
- [``](#otherwise>)
- [Transformation](#transformation-2)
- [For Tag](#for-tag)
- [Transformation](#transformation-3)
- [With Tag](#with-tag)
- [Transformation](#transformation-4)
- [Linting](#linting)
- [ESLint](#eslint)
- [FlowType](#flowtype)
- [Alternative Solutions](#alternative-solutions)
- [Pure JavaScript](#pure-javascript)
- [Conditionals](#conditionals)
- [Loops](#loops)
- [Comparison](#comparison)
- [React Components](#react-components)
- [What about Typescript](#what-about-typescript)
- [Major Versions](#major-versions)
- [I Want to Contribute!](#i-want-to-contribute)### A Note on Transformation and Alternative Solutions
It appears to be pretty easy to implement **conditionals as React component**, which is underlined by the amount
of libraries which have taken this approach. However, all of them suffer from the same major caveat: A React component
will always evaluate all of its properties including the component body. Hence the following example will fail for
those libraries:```javascript
{item.title}
```The error will be "Cannot read property 'title' of undefined", because React will evaluate the body of the custom
component and pass it as "children" property to it. The only workaround is to force React into lazy evaluation by
wrapping the statement in a function.This is the reason why conditionals must be implemented in pure JS. _JSX-Control-Statements_ only adds the
syntactic sugar to write conditionals as component, while it transforms this "component" to a pure JS expression.See [Alternative Solutions](#alternative-solutions) for a more detailed comparison and pure JS solutions.
## Installation
As a prerequisite you need to have [Babel](https://github.com/babel/babel) installed and configured in your project.
Install via npm:
```
npm install --save-dev babel-plugin-jsx-control-statements
```Then you only need to specify _JSX-Control-Statements_ as Babel plugin, which you would typically do in your `.babelrc`.
```
{
...
"plugins": ["jsx-control-statements"]
}
```If you use the `transform-react-inline-elements` plugin, place it _after_ `jsx-control-statements`:
```
{
...
"plugins": ["jsx-control-statements", "transform-react-inline-elements"]
}
```Babel can be used and configured in many different ways, so
[use this guide](https://github.com/AlexGilleran/jsx-control-statements/wiki/Installation) to pick a configuration
which fits your setup.## Syntax
### If Tag
Used to express the most simple conditional logic.
```javascript
// simpleIfBlock
// using multiple child elements and / or expressions
one
{ "two" }
three
four```
#### <If>
The body of the if statement only gets evaluated if `condition` is true.
| Prop Name | Prop Type | Required |
| --------- | --------- | ------------------ |
| condition | boolean | :white_check_mark: |#### _<Else /> (deprecated)_
The else element has no properties and demarcates the `else` branch.
This element is deprecated, since it's bad JSX/XML semantics and breaks auto-formatting.
Please use `` instead.#### Transformation
If statements transform to the _ternary operator_:
```javascript
// before transformationTruth
;// after transformation
{
test ? Truth : null;
}
```### Choose Tag
This is an alternative syntax for more complex conditional statements. Its a equivalent of `switch` statement for jsx. The syntax itself is XMLish and conforms by and
large to JSTL or XSLT (the attribute is called `condition` instead of `test`):```javascript
IfBlock
ElseIfBlock
Another ElseIfBlock
...
ElseBlock
// default block is optional; minimal example:
IfBlock
```
#### <Choose>
Acts as a simple container and only allows for `` and `` as children.
Each `` statement requires at least one `` block but may contain as many as desired.
The `` block is optional.#### <When>
Analog to ``.
| Prop Name | Prop Type | Required |
| --------- | --------- | ------------------ |
| condition | boolean | :white_check_mark: |#### <Otherwise>
`` has no attributes and demarcates the else branch of the conditional.
#### Transformation
This syntax desugars into a (sequence of) ternary operator(s).
```javascript
// Before transformation
IfBlock1
IfBlock2
ElseBlock
;// After transformation
{
test1 ? (
IfBlock1
) : test2 ? (
IfBlock2
) : (
ElseBlock
);
}
```### For Tag
Define `` like so:
```javascript
// you must provide the key attribute yourself
{ item.title }
// using the index as key attribute is not stable if the array changes
{ item }
Static Text
```| Prop Name | Prop Type | Required | description |
| --------- | ------------------------------ | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
| of | array or collection(Immutable) | :white_check_mark: | the array to iterate over. This can also be a collection (Immutable.js) or anything on which a function with the name `map` can be called |
| each | string | | a reference to the current item of the array which can be used within the body as variable |
| index | string | | a reference to the index of the current item which can be used within the body as variable |Note that a `` _cannot_ be at the root of a `render()` function in a React component, because then you'd
potentially have multiple components without a parent to group them which isn't allowed. As with ``, the same rules
as using `Array.map()` apply - each element inside the loop should have a `key` attribute that uniquely identifies it.#### For Tag - Alternative Syntax
For those using Typescript, the previous syntax introduces several issues with undefined variables. To deal with this issue, we introduce a following syntax, inspired by [tsx-control-statements](https://www.npmjs.com/package/tsx-control-statements).
```javascript
// before transformation
(
{index}. {item.title}
)}
/>;// after transformation
{
items.map(function(item, index) {
{index}. {item.title}
;
});
}
```| Prop Name | Prop Type | Required | description |
| --------- | ------------------------------ | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
| of | array or collection(Immutable) | :white_check_mark: | the array to iterate over. This can also be a collection (Immutable.js) or anything on which a function with the name `map` can be called |
| body | map expression | | expression of the map statement |### With Tag
Used to assign values to local variables:
```javascript
// simple{ foo }
{ bar }// nested
{ foo }
{ bar }
```
| Prop Name | Prop Type | Required | description |
| --------- | --------- | -------- | -------------------------------------------------------- |
| any name | any type | | assign prop value to a local variable named by prop name |You may assign multiple variables with a single `` statement. The defined variable is
available only within the `` block.#### Transformation
`` statements transform to immediately-invoked function expressions:
```javascript
// before transformation{foo}
;// after transformation
{
(function(foo) {
return {foo};
}.call(this, 47));
}
```## Linting
### ESLint
Since all control statements are transformed via Babel, no `require` or `import` calls are needed. This in turn
(well, and some more cases) would lead to warnings or errors by ESLint about undefined variables.But fortunately you can use this
[ESLint plugin for _JSX-Control-Statements_](https://github.com/vkbansal/eslint-plugin-jsx-control-statements)
to lint your code.### FlowType
There's still not a perfect solution for FlowType given that it doesn't provide a lot of plugin functionality
(at least not yet). Flow definitions are available in `jsx-control-statements.latest.flow.js` for Flow >= 0.53, or `jsx-control-statements.flow.js` (deprecated) for Flow < 0.53 - you can pick which file to use [like this](https://github.com/AlexGilleran/jsx-control-statements/pull/68#issuecomment-323562980). These will stop the
type checker complaining about statements being undeclared. As of now there's no neat way to make the Flow checker
recognise `each` attributes in `` loops as a variable - the best workaround for now is something like:```javascript
render() {
declare var eachVariable: string;return (
{eachVariable}
);
}
```If you know of a better way to work around this please let us know!
## Alternative Solutions
### Pure JavaScript
Since everything will be compiled to JavaScript anyway, you might prefer to stick to pure JavaScript solutions.
#### Conditionals
Probably the most common way for simple conditionals is the use of the && operator:
```javascript
// simple if
{
test && true;
}// additionally the else branch
{
!test && false;
}
```The ternary operator is probably more elegant for if / else conditionals:
```javascript
// simple
{
test ? true : false;
}// with multiple children
{
test ? (
[one, two]
) : (
false
);
}
```Another approach is to refactor your conditional into a function:
```javascript
testFunc(condition){
if(condition) {
return true;
}
else {
return false
}
}render() {
return (
{ testFunc(test) }
)
}
```#### Loops
Not many options here:
```javascript
{
items.map(function(item) {
{item.title};
});
}
```#### Comparison
Arguments pro _JSX-Control-Statements_ in comparison to pure JS solutions:
- More intuitive and easier to handle for designers and people with non-heavy JS background
- JSX does not get fragmented by JS statements
- Better readability and neatness, but that probably depends on youCons:
- Penalty on build-time performance
- Depends on Babel 6
- Some Babel configuration### React Components
There are a reasonable amount of React components for conditionals (e.g. [react-if](https://github.com/romac/react-if), which inspired this in the first place), _JSX-Control-Statements_ is the only approach we know of that avoids execution of all branches (see the [intro section](#a-note-on-transformation-and-alternative-solutions)), and there seems to be no other component-based solution to looping - while it would be possible to make a component that renders everything in `props.children` for every element of an array, you'd have to access the members of the array in that component instead of the one that uses it.
For more discussion on `If` in React by the react team, have a look at https://github.com/reactjs/react-future/issues/35.
To sum up:
- Conditionals don't execute invalid paths
- Loops with variable references to each element and index are made possible
- No penalty on runtime performance
- No import / require statements needed to use control statements
- It works exactly as JSX is supposed to work: Plain syntactic sugarCons:
- Depends on Babel 6
- Some Babel configuration
- Slightly longer build times
- Requires an extra plugin to work with ESLint## For macros
If you are looking to use macros with it, take a look at [jsx-control-statements-macros](https://github.com/akilansengottaiyan/jsx-control-statements) which is basically the extension of jsx-control-statements with macros support.## What about Typescript?
[There's a version for that by @KonstantinSimeonov!](https://github.com/KonstantinSimeonov/tsx-control-statements)
## Major Versions
- 4.x.x is a pure Babel plugin supporting Babel >= 7.
- 3.x.x is a pure Babel plugin supporting Babel >= 6.
- 2.x.x was a Babel plugin supporting Babel >= 6, and a set of JSTransform visitors.
- 1.x.x was a Babel plugin supporting Babel <= 5, and a set of JSTransform visitors.This used to support both JSTransform and Babel, but as JSTransform is no longer maintained support was dropped. You can
find the code for the JSTransform version at https://github.com/AlexGilleran/jsx-control-statements-jstransform.## I Want to Contribute!
Yay! Please read the [Contributor's Guide](https://github.com/AlexGilleran/jsx-control-statements/blob/master/CONTRIBUTING.md).