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

lloop

v1.0.0-beta.5

Published

A React JSX Loop component with the lloopy name.

Downloads

12

Readme

lloop

✨✨ NEW: Now supports React 16 arrays! ✨✨

A React JSX <Loop /> component with the lloopy name. It allows you to iterate through an array of items and render markup without switching context back and forth between JSX and JavaScript.

this might be a llama

(this might be a llama. his name is lloyd)

How do I get it?

Let's quickly get this out of the way. How do I get your aweome code?

$ npm i --save lloop

Now, what problem does it solve?

Let's say that you had an array of user objects that llooked like this.

const users = [
  { id: 100, name: 'Homer Simpson', sex: 'M', age: 36, imgUrl: 'homer.png' },
  { id: 101, name: 'Marge Simpson', sex: 'F', age: 34, imgUrl: 'marge.png' },
  { id: 102, name: 'Lisa Simpson', sex: 'F', age: 11, imgUrl: 'lisa.png' },
  { id: 103, name: 'Bart Simpson', sex: 'M', age: 8 ,imgUrl: 'bart.png' },
];

You want to render this llist of users. Traditionally you might go about this using map, something llike this:

<div>
  {
    users.map(user => {
      return <User key={user.id} {...user} />;
    })
  }
</div>

The User component would receive the following props: name, sex, age, and imgUrl. You find yourself context switching in and out of JSX, making the code cumbersome to read that stright JSX markup. But it's the way we've always done it, so it must be right.

But is there a better way?

Lloop can accomplish the same thing without popping between JSX and JavaScript.

<Loop items={users}>
  <User />
</Loop>

Loop automatically attaches the same props (one for each object key) that we passed in the JavaScript map example above, but it does so automatically, like magic, reducing visual clutter for a cleaner code.

Both render the exact same HTML which might look something like this:

simpsons screen shot

(see the code running live in this codesandbox)

What props can/must I pass to Loop?

| Prop Name | Description | | ---------- | ----------- | | items | An array of items to render (required) | | primaryKey | Name of the unique key for the array. If there is no unique key, set to null and the index of the item will be passed to React as the key. So while it is not required, it is recommended that you pass a primaryKey. | | as | The type of tag to use as the wrapped component. Defaults to as="div" if running React versions below 16. If running React 16 or greater, no wrapped component will be used. This may also be the name of a component. Example as={MyList}. | | itemKey | The prop to use if destructure is true. Defaults to item. | | indexKey | The prop to pass the loop index. Defaults to index. | | destructure | If set to true, each item will be destructured and passed as an individual prop (i.e. {...item}). If false, the item is passed as the prop defined by itemKey (i.e. item={item}). Defaults to true. |

Any other props passed to Loop will be passed on to the wrapping element. For example:

<Loop items={users} className="foo">
  ...

will produce

<div class="foo">
  ...

Shortcuts

Because we realize that there are several distinct use case for looping, we've created these shortcuts.

SimpleLoop

If you use the ul/li pattern, you can use the SimpleLoop shortcut.

import { SimpleLoop } from 'lloop';
const animals = ['dog','cat'];
...
<SimpleLoop items={animals}>
  <li />
</SimpleLoop>

which is functionally equivalent to

<Loop as="ul" destructure={false} itemKey="children" />

The example above will output this.

• dog
• cat

ItemLoop

If you with to pass the entire item object to your child without destructuring, you can use ItemLoop.

import { ItemLoop } from 'lloop';

const MyComponent = ({ item }) => {...};
...
<ItemLoop>
  <MyComponent />
</ItemLoop>

which is functionally equivalent to

<Loop destructure={false} />

Live Example

You can see an extensive live example running here on codesandbox.