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-smart-conditional

v1.0.21

Published

Manage conditional rendering in react js and it's frameworks like a pro

Downloads

14,588

Readme

npm npm bundle size License Contact

React Conditional Render

A flexible and reusable React component for conditional rendering.

Features

  • Conditional rendering of content based on boolean conditions
  • Support for multiple conditions with If components
  • Fallback rendering with Else component
  • Option to render single or multiple true conditions
  • Polymorphic component API for flexible element rendering

Table of Contents

Installation

pnpm install react-smart-conditional

Usage

Traditional React Conditional Rendering

The following example demonstrates the traditional way of conditional rendering in React using ternary operators and logical AND operators. While functional, this approach can become difficult to read and maintain as the number of conditions increases.

import React from 'react';

const DataDisplay = ({ isLoading, error, data }) => {
  return (
    <div>
      {isLoading ? (
        <p>Loading...</p>
      ) : error ? (
        <p>Error: {error.message}</p>
      ) : data ? (
        <div>
          <h1>Data Loaded:</h1>
          <pre>{JSON.stringify(data, null, 2)}</pre>
        </div>
      ) : (
        <p>No data available.</p>
      )}
    </div>
  );
};

export default DataDisplay;

Using React Conditional Render

This example showcases the same component using the react-smart-conditional library. The Show component and its child components (If, and Else) provide a more declarative and readable approach to conditional rendering, especially for complex scenarios.

import React from 'react';
import { Show } from 'react-smart-conditional';

const DataDisplay = ({ isLoading, error, data }) => {
  return (
    <Show as="section">
      <Show.If condition={isLoading}>Loading...</Show.If>
      <Show.If condition={error}>Error: {error.message}</Show.If>
      <Show.If condition={data}>
        <h1>Data Loaded:</h1>
        <pre>{JSON.stringify(data, null, 2)}</pre>
      </Show.If>
      <Show.Else>
        <p>No data available.</p>
      </Show.Else>
    </Show>
  );
};

export default DataDisplay;

Rendering Multiple True Conditions

To render all true conditions, use the multiple prop:

<Show multiple>
  <Show.If condition={condition1}>Content for condition 1</Show.If>
  <Show.If condition={condition2}>Content for condition 2</Show.If>
  <Show.Else>Fallback content</Show.Else>
</Show>

This will render both condition1 and condition2 if they are true.

API

  1. Show - Main container for conditional rendering

    • Props:
      • multiple: boolean (default: false) - When true, renders all true conditions. When false, renders only the first true condition.
      • as?: string | React.ComponentType - Wrapper element/component (optional, default: React.Fragment)
      • children: React.ReactNode - Should contain If, ElseIf, and Else components.
  2. Show.If - Renders children when condition is true

    • Props:
      • as?: string | React.ComponentType - Wrapper element/component (optional, default: div)
      • condition: boolean - Condition to evaluate (required)
      • children: React.ReactNode - Content to render if true
  3. Show.Else - Renders when all previous conditions were false

    • Props:
      • as?: string | React.ComponentType - Wrapper element/component (optional, default: div)
      • children: React.ReactNode - Content to render

Polymorphic API

The Show, Show.If, and Show.Else components support polymorphic rendering:

<Show as="section" className="container" data-testid="show-container">
  <Show.If
    condition={true}
    as="p"
    className="content active"
    onClick={() => console.log('Clicked')}
  >
    Paragraph content
  </Show.If>
  <Show.If
    condition={false}
    as="div"
    className="content inactive"
    style={{ display: 'none' }}
  >
    Hidden content
  </Show.If>
  <Show.Else as="span" className="fallback" aria-label="Fallback content">
    Span content
  </Show.Else>
</Show>

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

If you find this project helpful, please consider giving it a star on GitHub! ⭐️

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Made with ❤️ Wilson Adenuga - @Adenugawilson - [email protected]

Project Link: https://github.com/oluwatunmiisheii/react-smart-conditional