Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/eins78/react-let


https://github.com/eins78/react-let

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

# `react-let`

Version
License

Like `(let)` in Lisp, [Clojure](https://clojuredocs.org/clojure.core/let), etc., but for React/JSX.

Makes it easier to declare variables deep inside a JSX tree,
which are then passed to a `children` render function.

When used with TypeScript, the render function arguments are infered from the props given to ``.

## Installation

```shell
npm add react-let
# alternative:
yarn add react-let
```

## Usage Examples

```tsx
import React from "react";
import { Let } from "../main";

export const ExampleMemo = () => (



{({ result, another }) => ( // TypeScript infered: (parameter) result: string; (parameter) another: number


{result}. {another}.


)}


);

export const ExampleInput = () => (



{({ id }) => ( // TypeScript infered: (parameter) id: string
<>
{id}

>
)}


);

export const ExampleAsync = () => {
type FetchData = { str: string; num: number };
type FetchResult = { state: "loading" | "finished" | "error"; data?: FetchData };
const [fetchResult, setFetchResult] = useState({ state: "loading" });
return (


{fetchResult.state === "loading" ? (
"loading…"
) : fetchResult.state === "error" ? (
"error!"
) : (

{({ data }) => ( // TypeScript infered: (parameter) data: FetchData
<>
{JSON.stringify(data)}

>
)}

)}

);
};
```

All examples can be found in `src/example/App.tsx`,
and be can be run using:

```shell
npm ci
npm run dev
```

## Alternatives

Instead of using this component, you can use an [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) inside which you can declare JavaScript variables normally (i.e. you can use statements and not just expressions).

```tsx
export const ExampleVanilla = () => (


{(() => {
const result = "expensive-calculation-result";
const another = 42;

return (


{result}. {another}.


);
})()}

);
```

## Development

The command to run the examples could is used for development (but no work is currently planned as the component is feature-complete).

To publish a new version:

```shell
npm version
npm publish && \
git push --tags origin HEAD:main
```

## Copyright / License

© Max F. Albrecht

License: MIT