An open API service indexing awesome lists of open source software.

https://github.com/hc-oss/control-statements

Clean way to write loops and conditions in JSX ✨
https://github.com/hc-oss/control-statements

conditions loops ng-if reactjs

Last synced: 2 months ago
JSON representation

Clean way to write loops and conditions in JSX ✨

Awesome Lists containing this project

README

          

[![npm version](https://img.shields.io/npm/v/control-statements.svg?style=flat-square)](https://www.npmjs.com/package/control-statements)
[![Build Status](https://travis-ci.org/harshzalavadiya/control-statements.svg?branch=master)](https://travis-ci.org/harshzalavadiya/control-statements)

There's no built-in looping and conditional syntax in React. This library adds the syntactic sugar to write looping and conditionals as component.

Fork of [react control statements](https://www.npmjs.com/package/react-control-statements) but with `` implementation

## Free Advise

If you are coming from non-jsx background you might think this package is a good start but in reality 🤫 *this package is not needed at all.*

because, it's easy to replicate this scenarios with pure ES6+, I'm putting some examples that might help you for getting started.

### Pure React Conditional Statements
[![Edit SimpleConditionalStatements](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/peaceful-bhaskara-q35fy?fontsize=14)
```jsx
import React from "react";

export default function PureReactExamples() {
const foo = true;
const fruits = ["🍇 Grapes", "🍈 Melon", "🍌 Banana"];
return (
<>

Simple If without Else


{foo &&

Yay, Foo is true

}

Simple If with Else


{foo ?

Yay, Foo is true

:

Whoa, Foo is false

}

Loop Example


{fruits.map((fruitName, index) => (

{fruitName}


))}
>
);
}
```

---

## Install

```
npm install --save control-statements
```

## Usage

### If

The body of the if statement only gets evaluated if condition is true.

```jsx
import React, { Component } from "react";
import { If } from "control-statements";

class YourComponent extends Component {
render() {

Truth
;
}
}
```

### Choose

This is an alternative syntax for more complex conditional statements.

```jsx
import React, { Component } from "react";
import { Choose, When, Otherwise } from "control-statements";

class YourComponent extends Component {
render() {
return (




IfBlock


ElseIfBlock
Another ElseIfBlock
...


ElseBlock



);
}
}
```

### For

For syntax.

```jsx
import React, { Component } from "react";
import { For } from "control-statements";

class YourComponent extends Component {
render() {
return (



$item.title


);
}
}
```