Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yosbelms/react-deco
:hotsprings: React utility belt to write functional components and declarative JSX code
https://github.com/yosbelms/react-deco
declarative if jsx map pure react reactjs view
Last synced: 25 days ago
JSON representation
:hotsprings: React utility belt to write functional components and declarative JSX code
- Host: GitHub
- URL: https://github.com/yosbelms/react-deco
- Owner: yosbelms
- Created: 2018-07-01T21:03:13.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-10T05:49:15.000Z (about 2 years ago)
- Last Synced: 2024-04-24T00:29:35.106Z (8 months ago)
- Topics: declarative, if, jsx, map, pure, react, reactjs, view
- Language: TypeScript
- Homepage:
- Size: 185 KB
- Stars: 30
- Watchers: 5
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# React Deco
*React Deco* Give back to JSX what is JSX’s
* [Overview](#overview)
* [Installation](#installation)
* [Usage](#usage)
* [Components](#components)
* [If](#if)
* [Switch/When](#switch/when)
* [Map](#map)
* [Bare](#bare)
* [Await](#await)## Overview
*React Deco* is a library that aims to make React complex views more declarative, idiomatic, easy to read, and easy to write, by consequence more mantainables.
This library takes advantage of Render-Props pattern (effectively used by [React Router](https://reacttraining.com/react-router/web/api/Route) and [Downshift](https://github.com/paypal/downshift)) to make possible to write conditionals and loops in a more declarative way while reducing visual cluttering.
Lets write a simple table of products with two columns `Name` and `In Stock`. If `In Stock` is `0` then a message `Out of Stock` should be displayed. Currently we should write something like the following:
```tsx
function ProductTable({products}) {
return (
Name
In Stock
{renderTableBody(products)}
)
}function renderTableBody(products) {
return (
{products.map((product) =>
{product.name}
{(product.inStock > 0)
? {product.inStock}
: Out of Stock
}
)}
)
}
```This library will turn the above code into:
```tsx
function ProductTable({products}) {
return (
Name
In Stock
{product.name}
0}
then={{product.inStock}}
else={Out of Stock}
/>
}/>
)
}
```## Installation
```
// with yarn
yarn add react-deco// with npm
npm install react-deco
```## Usage
```ts
// ES2015+ and TS
import {If, Map, Bare} from 'react-deco'// CommonJS
var ReactDeco = require('react-deco')
var If = ReactDeco.If
var Map = ReactDeco.Map
var Bare = ReactDeco.Bare
```## Components
React-Deco exports some primitives wich holds reusable logic, to help developers to write *presentational logic* in JSX.
### If
Conditionally render components based on the truthy-ness of evaluating the `test` prop. Render `then` if `test` evaluates to truthy, render `else` otherwise.
```tsx
b}
then={'a is greater then b'}
else={'a is not greater than b'}
/>
```Passign functions in `then` and `else` makes the rendering process more efficient because only one of both branches is evalueted depending on truthy-ness of `test`. See [Short Circuit Evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation)
```tsx
b}
then={() => 'a is greater then b'}
else={() => 'a is not greater than b'}
/>
```### Switch/When
Render the first `When` child whose`test` prop evaluates to `true`.
```tsx
1} render={() =>
Foo} />
Default} />```
### Map
Render the result of dispatching to the `map` method of `target` passing the `with` function as the first argument.
```tsx
{item}
} />
```### Bare
> React Hooks addresses the same problem that this component was created for.
A component that its `constructor`, `shouldComponentUpdate`, and lifecycle methods can be assigned via props
```tsx
...
} />
````Bare` componets accept the following props:
* `render`: `render(self)`
* `constructor`: `constructor(self, props, ctx)`
* `didCatch`: `didCatch(self)`
* `didMount`: `didMount(self)`
* `didUpdate`: `didUpdate(self, prevState)`
* `shouldUpdate`: `shouldUpdate(self, nextState)`
* `willUnmount`: `willUnmount(self)`Additionaly, `Bare` components accepts a prop named `pureBy`. In case this property is provided the passed value will be used to compute the component purity using shallow comparison, if it is an array the shallow comparison will be computed by shallow-comparing each value in the array.
```tsx
{client.name}
{client.age}
} />
```The above code will re-render only if one of the properties of the `client` object is different.
Bare components also contains a self reference called `self`, frequently useful to keep a reference to the component while destructuring in lifecycle params, example:
```tsx
// use self
} />
````setState` can be used inside the constructor
### Await
Render components based on the state of a promise. Renders `then` prop when the promise is resolved. Renders `catch` prop when the promise is rejected. Renders `placeholder` while the promise is not resolved nor rejected.
```tsx
const usersPromise = fetch('users')...
} />
````Await` componets accept the following props:
* `promise`
* `then`
* `catch`
* `placeholder`Published under MIT Licence
(c) Yosbel Marin 2018