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

kill-jsx

v1.1.0

Published

Allows to use if-else, loops and other JS features in JSX.

Downloads

5

Readme

Kill JSX

This library provides component to use JavaScript features in JSX. It's necessary if you have some conditions in your JSX. By default React using conditional operators, ternary operator and Array.map method to solve such problems. But this lib give you more beautiful solution.

React by default:

export function Component(props) {
   return (
      <div>
         {props.list
         ? props.list.map((item, i) => (
            <Another item={item} key={`another#${i}`} />
         ))
         : <Fallback />}
      </div>
   );
}

Using kill-jsx:

import __ from 'kill-jsx';

export function Component(props) {
   return (
      <div>
         <__>{(o, i) => {
            if (props.list) {
               for (let item of props.list) {o(
                  <Another item={item} key={`another#${i++}`} />
               )}
            } else {o(
               <Fallback />
            )}
         }}</__>
      </div>
   );
}

Reference

  1. Kill-jsx with component (^1.0.0)
  2. Kill-jsx with exit function (^1.1.0)

1. Kill-jsx with component

How to use?

  1. Import __ component from 'kill-jsx' package
  2. In target place use <__>{/*some callback here*/}</__>
  3. Callback takes two arguments: output function and counter.

About callback

Better readability reached when you name output function as o. In this case outputting of JSX parts looks like you use sad cyclops operator - o( <JSXHere/> ).

Another argument is counter. It supplied for for-of loops, where you need to use counters for key prop of components. Recommended names are: i, j, k - for nested usage of kill-jsx.

Don't use spaces!!!

Important thing! Don't use spaces after <__> and before </__> - it will cause errors.

No spaces example

2. Kill-jsx with exit function

This case is kind of shorter and readable. Idea is simple - let's use function instead of component!

Example:

import { exit } from 'kill-jsx';

export function Component(props) {
   return (
      <div>
         {exit((o, i) => {
            if (props.list) {
               for (let item of props.list) {o(
                  <Another item={item} key={`another#${i++}`} />
               )}
            } else {o(
               <Fallback />
            )}
         })}
      </div>
   );
}

Simply exit from JSX back to JavaScript. Any JS features are available, but best practice is to use only conditions and loops, to reach better readability.

One more good feature - easy injection of console.log or other logger while you develop.