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-condify

v1.0.1

Published

A flexible library for managing conditional rendering of components in React applications with a simple and intuitive syntax.

Downloads

1

Readme

React Conditional Render npm version

A flexible library for managing conditional rendering of components in React applications with a simple and intuitive syntax.

Created with ❤️ for React developers who need a clear, reusable way to handle conditional rendering.

Installation

To install the library, you can use either npm or yarn:

npm install react-condify

or

yarn add react-condify

Components

This library provides two main components for conditional rendering:

  1. Cond: Basic conditional rendering for synchronous operations.
  2. AsyncCond: Conditional rendering for asynchronous operations with support for caching and polling.

Cond Component

The Cond component is used to render a specific component based on a condition or an array of conditions.

Props

  • condition (optional): A boolean value determining whether to render the component in then or fallback.
  • then (optional): The component that will be rendered if condition is true.
  • cases (optional): An array of condition objects, where each object contains a condition and a then component to render.
  • fallback (optional): The component to render if no conditions are met or condition is false.

Example

Simple usage with condition

import React from "react";
import { Cond } from "react-condify";

const MyComponent = () => {
  const isLoggedIn = true;

  return (
    <Cond condition={isLoggedIn} then={<Dashboard />} fallback={<Login />} />
  );
};

Usage with multiple conditions using cases

import React from "react";
import { Cond } from "react-condify";

const MyComponent = () => {
  const status = "loading";

  return (
    <Cond
      cases={[
        { condition: status === "loading", then: <Loading /> },
        { condition: status === "error", then: <Error /> },
      ]}
      fallback={() => <Data />}
    />
  );
};

AsyncCond Component

The AsyncCond component provides conditional rendering based on asynchronous operations, such as API requests. It supports caching and polling for repeated requests.

Props

  • asyncFunction: A function that returns a promise. The result of this promise will determine which component to render.
  • then: A render prop that takes the resolved data as an argument and returns the component to render.
  • else: The component to render if the async function returns no result (empty).
  • loading: The component to render while the async function is in progress.
  • error: The component to render if the async function throws an error.
  • polling (optional): A number (in seconds) to periodically re-execute the async function. Defaults to no polling.
  • cacheKey (optional): A string key to store the result of the async function in localStorage. If the result is cached, it will be retrieved from cache on subsequent renders.
  • onError (optional): A function to handle any errors that occur during the async operation.

Example

Simple usage without polling or caching

import React from "react";
import { AsyncCond } from "react-condify";

const MyComponent = () => {
  const fetchData = () => fetch("/api/data").then((res) => res.json());

  return (
    <AsyncCond
      asyncFunction={fetchData}
      then={(data) => <Data data={data} />}
      else={<NoData />}
      loading={<Loading />}
      error={<Error />}
    />
  );
};

Usage with polling and caching

import React from "react";
import { AsyncCond } from "react-condify";

const MyComponent = () => {
  const fetchData = () => fetch("/api/data").then((res) => res.json());

  return (
    <AsyncCond
      asyncFunction={fetchData}
      then={(data) => <Data data={data} />}
      else={<NoData />}
      loading={<Loading />}
      error={<Error />}
      polling={60} // Poll every 60 seconds
      cacheKey="myDataKey" // Cache the data with this key
    />
  );
};

Error handling

If you want to catch and log errors that happen during the async function execution, you can pass an onError callback:

import React from "react";
import { AsyncCond } from "react-condify";

const MyComponent = () => {
  const fetchData = () => fetch("/api/data").then((res) => res.json());

  return (
    <AsyncCond
      asyncFunction={fetchData}
      then={(data) => <Data data={data} />}
      else={<NoData />}
      loading={<Loading />}
      error={<Error />}
      onError={(err) => console.error("An error occurred:", err)}
    />
  );
};

License

This project is licensed under the MIT License.


Badges

npm version Node.js CI

Links

See also: