@naderikladious/react-flow
v1.0.1
Published
Declarative control-flow primitives for React (Condition/If/Else, For/ForEach, Batch) with TypeScript types.
Maintainers
Readme
Utilities for declarative control-flow primitives in React, written in TypeScript and packaged for ESM/CJS consumers.
Motivation
- Keep conditional and iterative UI logic readable without custom hooks per component.
- Provide type-safe, render-prop based building blocks (
Flow.Condition,Flow.If,Flow.ForEach, etc.). - Ship tree-shakeable named exports for both bundlers and Node targets.
Demo preview (GIF):
Installation
npm install @naderikladious/react-flowIf working from this repo locally, install and build the library first:
npm install
npm run buildTo explore the Vite example app:
cd example
npm install
npm run dev
# For GitHub Pages build: GITHUB_PAGES=true npm run buildAPI
Preferred namespace is Flow (with ReactFlow available as an alias).
Flow.Condition— provides a boolean value to descendants.Flow.AsyncCondition— resolves a boolean asynchronously and provides it to descendants.Flow.If— renders children when the condition is true (prop overrides context).Flow.Else— renders children when the condition is false (prop overrides context).Flow.Unless— renders children when the condition is false (prop overrides context).Flow.ForEach<T>— iterates an array with an optionalkeyExtractorand render-function children(item, index) => ReactNode.Flow.For— iteratescounttimes with optionalstartandstep, render-function children(index) => ReactNode.Flow.Batch<T>— groups items into chunks ofbatchSizeand renders(batch, batchIndex) => ReactNodeusingFlow.ForEachunder the hood.Flow.Switch/Flow.Case/Flow.Default— declarative branching on a single value.useFlowCondition— hook to read the nearestFlow.Conditionvalue (throws if missing).
All components are also available as named exports (tree-shakeable) in addition to the namespace object.
Examples
Basic conditionals:
<Flow.Condition value={isEnabled}>
<Flow.If>
<div>Enabled</div>
</Flow.If>
<Flow.Else>
<div>Disabled</div>
</Flow.Else>
</Flow.Condition>Lists and batches:
<Flow.ForEach items={items} keyExtractor={(item) => item.id}>
{(item) => <div>{item.name}</div>}
</Flow.ForEach>
<Flow.Batch items={products} batchSize={3}>
{(batch, i) => (
<section>
<h3>Batch {i + 1}</h3>
{batch.map((product) => (
<div key={product.id}>{product.title}</div>
))}
</section>
)}
</Flow.Batch>Numeric loops:
<Flow.For count={5} start={1}>
{(index) => <span key={index}>{index}</span>}
</Flow.For>Scripts
npm run build— bundle todist/(CJS, ESM, and.d.ts).npm run clean— clear and recreatedist/.npm run lint— placeholder.
Project structure
src/— library source.dist/— build output (CJS, ESM, types).example/— Vite app consuming the local package viafile:...
