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

react-render-iff

v3.0.0

Published

A helper component to render react components conditionally.

Downloads

703

Readme

react-render-iff

carbon (34)

NPM JavaScript Style Guide

Install

npm install --save react-render-iff

Usage

Import as "RenderIf" and use as illustrated in the examples below. There are many more powerful usages that may not be illustrated, so take a look at the source code to see what more you can do.

import React, { Component } from "react";

import RenderIf from "react-render-iff";

class Example extends Component {
  render() {
    const isTrue = 1 == 1;
    const isFalse = 1 == 2;

    const ExampleComp = props => {
      return <div {...props}>My wrapper comp stuff: {props.children}</div>;
    };

    return (
      <div>
        <h1>Example uses</h1>

        <div>
          <h2>Example 1:</h2>
          <p>
            Provide render conditions and render props.
            <br />
            <br />
            The "if" will succeed and render its render prop, "elseIfRender" as a
            plain Text node.
          </p>
          <RenderIf
            if={isTrue}
            render={"Hello, world. The expression evaluated to true 😎"}
            elseIf={() => 1 === 1}
            elseIfRender={() => "This will do!"}
            else={"Bye, world. Both expressions evaluated to false 😥"}
          />

          <div>
            <h3>🌴 The output of this example should be:</h3>
            Hello, world. The expression evaluated to true 😎
          </div>
        </div>

        <div>
          <h2>Example 2:</h2>
          <p>
            Render consitions and render props with a child node for the "if"
            condition.
            <br />
            <br />
            The "if" will succeed and render the child node.
          </p>
          <RenderIf
            if={() => isTrue}
            elseIf={() => 1 === 1}
            elseIfRender={() => "This will do!"}
            else={() => (
              <p>Bye, world. Both expressions evaluated to false 😥</p>
            )}
          >
            Hello, world. The expression evaluated to true 😎
          </RenderIf>

          <div>
            <h3>🌴 The output of this example should be:</h3>
            Hello, world. The expression evaluated to true 😎
          </div>
        </div>

        <div>
          <h2>Example 3:</h2>
          <p>
            The "elseIf" will succeed and render its "elseIfRender" prop, which
            happens to be an arrow function that will be evaluated.
          </p>
          <RenderIf
            if={() => isFalse}
            elseIf={() => 1 === 1}
            elseIfRender={() => "This will do!"}
            else={() => (
              <p>Bye, world. Both expressions evaluated to false 😥</p>
            )}
          >
            Hello, world. The expression evaluated to true 😎
          </RenderIf>

          <div>
            <h3>🌴 The output of this example should be:</h3>
            This will do!
          </div>
        </div>

        <div>
          <h2>Example 4:</h2>
          <p>
            You can use an arrow function to evaluate conditionals as well as
            for rendering.
            <br />
            <br />
            The "if" will succeed.
          </p>
          <RenderIf
            if={() => isTrue}
            elseIf={() => 1 === 1}
            elseIfRender={() => "This will do!"}
            else={() => (
              <p>Bye, world. Both expressions evaluated to false 😥</p>
            )}
          >
            <p>Hello, world.</p>
            <p>The expression evaluated to true 😎</p>
          </RenderIf>

          <div>
            <h3>🌴 The output of this example should be:</h3>

            <p>Hello, world.</p>
            <p>The expression evaluated to true 😎</p>
          </div>
        </div>

        <div>
          <h2>Example 5:</h2>
          <p>
            ✨ You can provide an "as" prop to render a parent element with the
            properties you provide.
            <br />
            <br />
            The "as" prop supports all HTML elements and any react components you
            provide to it. If you choose to provide a react component, your component
            must render the "props.children" that will be provided to it.
            <br />
            <br />
            The "elseIf" will succeed. "<RenderIf />" will be rendered as a "
            <section />" node with the provided classes and style, as well as any
            other allowed properties; the properties specific to "
            <RenderIf />" will be ignored when rendering.
          </p>

          <RenderIf
            as="section"
            className="u-textColorRed u-marginBottom10"
            style={{ background: "blue" }}
            title="Some cool stuff!"
            if={() => isFalse}
            elseIf={isTrue}
            elseIfRender={<p>This will do!</p>}
            else={() => (
              <p>Bye, world. Both expressions evaluated to false 😥</p>
            )}
          >
            <p>Hello, world.</p>
            <p>The expression evaluated to true 😎</p>
          </RenderIf>

          <div>
            <h3>🌴 The output of this example should be:</h3>

            <section
              class="u-textColorRed u-marginBottom10"
              style="background: blue;"
              title="Some cool stuff!"
            >
              <p>This will do!</p>
            </section>
          </div>
        </div>

        <div>
          <h2>Example 6:</h2>
          <p>
            Here, we provide a react component to the "as" prop. If you choose
            to provide a react component, your component must render the
            "props.children" that will be provided to it.
            <br />
            <br />
            The "else" will succeed. "<RenderIf />" will be rendered as a "
            <ExampleComp />" node with the provided classes and style, as well
            as any other allowed properties; the properties specific to "
            <RenderIf />" will be ignored when rendering.
          </p>

          <RenderIf
            as={ExampleComp}
            className="u-textColorRed u-marginBottom10"
            style={{ background: "blue" }}
            title="Some cool stuff!"
            if={() => isFalse}
            elseIf={isFalse}
            elseIfRender={<p>This will do!</p>}
            else={() => (
              <p>Bye, world. Both expressions evaluated to false 😥</p>
            )}
          >
            <p>Hello, world.</p>
            <p>The expression evaluated to true 😎</p>
          </RenderIf>

          <div>
            <h3>🌴 The output of this example should be:</h3>

            <div
              class="u-textColorRed u-marginBottom10"
              style="background: blue;"
              title="Some cool stuff!"
            >
              My wrapper comp stuff:
              <p>Bye, world. Both expressions evaluated to false 😥</p>
            </div>
          </div>
        </div>

        <div>
          <h2>Example 7:</h2>
          <p>
            ⚠️ You can specify whether to evaluate all conditions and render
            props safely by providing a truthy value to the "safeEval" prop.
            <br />
            <br />
            If an error occurs while evaluating the conditions, a warning will be
            printed to the console with the value of "safeEval" as the key for debugging.
            This only works when the conditionals and render props are provided as
            arrow functions, so keep that in mind.
            <br />
            <br />
            This can come in handy when rewriting your AngularJs app in React
            because AngularJs evaluates its "ng-if" and "ng-show" directives
            safely, which you would be accustomed to and thrown off by the lack
            their of when transitioning to React.
            <br />
            <br />
            The "if" condition's function throws an error for some reason, so it
            evaluates to false and we move on to the next condition. The "elseIf"
            condition evaluates to true.
          </p>
          <RenderIf
            as="header"
            if={() => {
              throw new Error(
                "Oh no! An error occurred while evaluating the if condition!"
              );
            }}
            elseIf={() => isTrue}
            elseIfRender={<p>This will do!</p>}
            else={() => (
              <p>Bye, world. Both expressions evaluated to false 😥</p>
            )}
            safeEval={"my-safe-eval-debug-key"}
          >
            <p>Hello, world.</p>
            <p>The expression evaluated to true 😎</p>
          </RenderIf>

          <div>
            <h3>🌴 The output of this example should be:</h3>

            <header>
              <p>This will do!</p>
            </header>
          </div>
        </div>
      </div>
    );
  }
}

License

By Lincoln W Daniel

MIT © Lwdthe1