npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reloc

v1.2.4

Published

React logic (control statements) components

Downloads

13

Readme

React logic components

npm License Contact

Provide the React control statements components <If>, <Switch>, <For>

Installation

npm:

npm i reloc

yarn:

yarn add reloc

API

1. Simple condition

<If>

| Property | Type | Required | |----------------------|---------------------------|----------| | check | Boolean | yes | | then or children | ReactNode, Function, null | no |

Example 01:

import { If } from  'reloc';

<If check={ obj } then={() => (
  <span>It is done</span>
)} />

or syntax:

<If check={status === DONE}>
  {() => (
    <span>It is done</span>
  )}
</If>

or unsafe syntax (Not recommend, see react issue 35):

<If check={status === DONE}>
  <span>It is done</span>
</If>

2. Complex conditional, Switch statements

<Switch>

Only the first case that satisfies the condition will be rendered.

| Property | Type | Required | Default | Description | |--------------|-------------------------|----------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | match | Boolean, Number, String | no | | If the match has set value, the component works as a 'Switch mode.' Otherwise, it works as a complex conditional mode. | | strict | Boolean | no | true | Only available for 'Switch mode.' If strict is true, it will check the data type of the match when comparing with the case value. See the example below for a better understanding. |

<Case>

| Property | Type | Required | |----------------------|---------------------------|----------| | check | Boolean | yes | | then or children | ReactNode, Function, null | no |

<Default>

| Property | Type | Required | |----------------------|---------------------------|----------| | - | | | | then or children | ReactNode, Function, null | no |

Example 02 - Complex condition:

import {Switch, Case, Default} from  'reloc';

<Switch>
  <Case check={status === DOING} then={() => (
    <span>DOING</span>
  )} />
  <Case check={status === DONE} then={() => (
    <span>DONE</span>
  )} />
  <Default then={() => (
    <span>OTHER</span>
  )} />
</Switch>
import {Switch, Case, Default} from  'reloc';

<Switch>
  <Case check={status === DOING}>
    {() => (
      <span>DOING</span>
    )}
  </Case>
  <Case check={status === DONE}>
    {() => (
      <span>DONE</span>
    )}
  </Case>
  <Default>
    {() => (
      <span>OTHER</span>
    )}
  </Default>
</Switch>
import {Switch, Case, Default} from  'reloc';

<Switch>
  <Case check={status === DOING}>
    <span>DOING</span>
  </Case>
  <Case check={status === DONE}>
    <span>DONE</span>
  </Case>
  <Default>
    <span>OTHER</span>
  </Default>
</Switch>

Example 03: Switch mode:

import {Switch, Case, Default} from  'reloc';

<Switch match={status}>
  <Case check={DOING} then={() => (
    <span>DOING</span>
  )} />
  <Case check={DONE} then={() => (
    <span>DONE</span>
  )} />
  <Default then={() => (
    <span>OTHER</span>
  )} />
</Switch>

Deferred syntax, alternative syntax similar to example 02.

Example 04: Switch mode with the strict prop off:

import {Switch, Case, Default} from  'reloc';

<Switch match={1} strict={false}>
  <Case check={'1'} then={() => (
    <span>Passed</span>
  )} />
  <Case check={'2'} then={() => (
    <span>Not passed</span>
  )} />
  <Default then={() => (
    <span>Not passed</span>
  )} />
</Switch>

Deferred syntax, alternative syntax similar to example 02.

3. Loop

<For>

Support Array, Set, Map, Object data types.

| Property | Type | Required | Description | |------------|----------------------------------------------------------------------------|----------|-----------------------------------------------------------------------------------------------| | items | Array, Map, Set, Object | yes | | | children | Function: (item: any, key: String|Number, index: Number) => ReactNode | yes | If the data type of the items is an Array or Set, the 'key' value will reference the index. |

Example 05:

import {For} from  'reloc';

<For items={items}>
  {(item, key, index) => (
    <span key={key}>{index}: {item.name}</span>
  )}
</For>

or unsafe syntax:

<For items={items} children={(item, key, index) => (
  <span key={key}>{index}: {item.name}</span>
)} />

Important: Deferring evaluation of children

It's crucial to understand that in JavaScript, which is an eagerly evaluated language, the code inside both the <If>, <Case>, <Default>, and <For> components will be executed even if the condition turns out to be false.

More specifically, the following code will throw an error obj is not defined:

<If check={ obj }>
  <span>{ obj.attr }</span>
</If>

To fix this issue, the code should be written like this:

<If check={ obj }>
  {() =>  (
    <span>{ obj.attr }</span>
  )}
</If>

or alternative syntax:

<If check={ obj } then={() => (
  <span>{ obj.attr }</span>
)} />

Therefore, for safety and efficiency reasons, it's recommended to use arrow functions for the child components of <If>, <Case>, <Default>, <For>.

For more discussion on If in React by the react team, have a look at https://github.com/reactjs/react-future/issues/35.

Alternative Solutions

As mentioned above, this package doesn't always run with the cleanest and most readable syntax. You'll need to use arrow functions for cases where children have complex logic to ensure safety.

So, is there any solution for a more comprehensive implementation of control statements in JSX? The answer is YES. You can refer to the following packages:

These are packages I really like but have to be cautious about due to the following limitations:

  • Compatibility: They only support a specific transpiler (babel, tsx). As of the current date (2024-01-06), jsx-control-statements doesn't work with popular bundlers like Vite, esbuild, microbundle, etc.
  • Long-term support: Solutions using React components to implement control statements will remain compatible with newer React versions as long as React ensures backward compatibility. Projects based on transpiler plugins may need updates when a new transpiler version is released.
  • IDE lacks code highlighting support.