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

@ferstack/react-for

v1.0.1

Published

This package provides a utility component that simplifies the usage of .map() function in React. It allows you to iterate over an array of items and render a component for each item, with the flexibility of providing a fallback component in case the array

Downloads

11

Readme

For Component for React

This package provides a utility component that simplifies the usage of .map() function in React. It allows you to iterate over an array of items and render a component for each item, with the flexibility of providing a fallback component in case the array is empty and also automatically assign keys with the identifier prop.

Install

You can add this component to your project by simply copying the code above into a new .tsx file in your project. There are no additional dependencies needed.

Usage

Here's an example of how to use the For component:

import { For } from './For';

const fruits = ['Apple', 'Banana', 'Cherry'];

function App() {
  return (
    <For each={fruits} fallback={<div>No fruits found.</div>}>
      {(fruit, index) => (
        <div key={index}>{fruit}</div>
      )}
    </For>
  );
}

export default App;

In this example, the For component will render a div for each fruit in the fruits array. If the array were empty, it would render the "No fruits found." message.

Using the identifier prop:

import { For } from './For';

const people = ['John', 'Luke', 'Matthew'];

function App() {
  return (
    <For each={fruits} fallback={<div>No fruits found.</div>}>
      {(fruit) => <p>{fruit}</p>}
    </For>
  );
}

export default App;

API

Props

  • each (required): An array of items to iterate over. Each item will be passed to the child function as an argument.
  • children (required): A function that returns a JSX element. This function will be called once for each item in the each array, and it receives the current item and its index as arguments.
  • identifier (optional): A string that will be used to generate a unique key for each child component. The key will be a concatenation of the identifier and the index. This prop is recommended when your children don't have stable keys.
  • fallback (optional): A JSX Element that will be rendered in case the each array is empty.

Why use this?

If you often find yourself mapping over arrays to render lists of components in React, this utility component can help you reduce boilerplate and keep your code cleaner and easier to read. It also handles edge cases like empty arrays and missing keys, so you don't have to worry about them.