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-dynamic-elements

v1.0.3

Published

To control visibility of elements in a React component dynamically based on the user attributes and the conditions available in page details

Downloads

1

Readme

React Dynamic Elements [react-dynamic-elements]

The React Dynamic Elements package is designed to selectively render child components based on specific conditions. It's particularly useful in scenarios where we need to control the visibility or behavior of components based on dynamic data from an API or user attributes.

This readme provides insights into how to use the component, its purpose, and scenarios where it can be beneficial.

Table of Contents

  1. Introduction
  2. Usage
  3. Sample JSON Structures
  4. Implementation
  5. Scenarios

Introduction

The React Dynamic Elements package is a versatile tool for creating dynamic, data-driven user interfaces in React applications. It allows us to control the visibility and behavior of components based on conditions and user attributes, making our application more versatile and adaptable to various scenarios.

Usage

To use the React Dynamic Elements package, install using below command

npm i react-dynamic-elements

We can wrap the RenderOnlyFor component available from package around the components which we want to conditionally render.

import { RenderOnlyFor } from "react-dynamic-elements";
....

 <RenderOnlyFor
            userAttributes={userAttributes}
            pageDetails={pageConditions}
        >
         {rest of the component}
         
         <p actionId="recent">
           Element to be displayed conditionally.
          </p>
          
          {rest of the component}
</RenderOnlyFor>

The key feature is defining an actionId prop for each child component, which corresponds to the conditions received from API. The actionId prop is used to determine when each component should be rendered based on the evaluated conditions.

Sample JSON Structures

Here are sample JSON structures representing user attributes and page conditions:

User Attributes:

{
  "age": 35,
  "salary": "proprietorship",
  "monthly_salary": 1250000,
  "emi_count": 13,
  "average_balance": 120000,
  "transaction_frequency": 2,
  "last_transaction": 3,
  "address": {
    "country": "india"
  }
}

Page Conditions Received from API:

[
  {
    "id": 1,
    "actionId": "recent",
    "condition": "(age < 30) && (age > 20)"
  },
  {
    "id": 2,
    "actionId": "entry",
    "condition": "(emi_count < 30) && (age > 20)"
  },
  {
    "id": 3,
    "actionId": "content",
    "condition": "(address.country === \"netherlands\")"
  },
  {
    "id": 4,
    "actionId": "payment",
    "condition": false
  }
]

These structures are used to determine when and how components with corresponding actionId values should be rendered by the RenderOnlyFor component.

Implementation

The RenderOnlyFor component matches the userAttributes data and evaluates conditions associated with each component's actionId with the pageDetails. If a condition is met, the corresponding component is rendered; otherwise, it remains hidden.

The component also supports conditional rendering using user attributes and custom conditions. It can handle both boolean and string-based conditions, offering a high degree of flexibility in building dynamic UIs.

Scenarios

1. Single Component

You can use the RenderOnlyFor component for conditional rendering in a single component. For example, if you have a page with different elements, some of which should be displayed only if certain conditions are met, you can use the actionId prop to control their visibility.

<p actionId="entry">Welcome to Portal</p>
<div>
  <p actionId="content">Welcome</p>
  <div>
    <p actionId="recent">To Component</p>
  </div>
  <p>Other Content</p>
</div>

In this scenario, components with actionId are rendered based on the conditions received from the API, while others are always displayed.

2. Using Reusable Components

Consider s reusable component as the Charts component which is designed to be a reusable component that adapts its behavior and content based on the pageId and associated conditions using the getActionId function:

import React from 'react';

function Charts({ pageId }) {
  // Define a function to determine the actionId based on the pageId
  const getActionId = () => {
    switch (pageId) {
      case "dashboard":
        return "dashboardAction";
      case "cart":
        return "cartAction";
      default:
        // Set a default actionId for unknown pages
        return "defaultAction";
    }
  };

  // Get the actionId dynamically
  const actionId = getActionId();

  return (
    <div>
      <h2>Charts Component</h2>
      <RenderOnlyFor pageId={pageId}>
        <div>
          {/* Use the dynamically determined actionId */}
          <p actionId={actionId}>Display specific data based on pageId</p>
        </div>
      </RenderOnlyFor>
    </div>
  );
}

export default Charts;

[We have used a single actionId here, if required we can make changes to the getActionId function to return different actionId for different elements.]

Summary

The RenderOnlyFor component is a powerful tool for creating dynamic, data-driven user interfaces in React applications. It allows us to conditionally render components based on conditions received from an API, which can be evaluated against user attributes. These conditions are highly dynamic and can vary based on different pages, users, or elements, enabling the creation of adaptive interfaces that respond to user characteristics and actions.

Benefits

  • Improved user experience through personalized content delivery.
  • Enhanced user engagement by showing relevant components or information.
  • Increased flexibility in designing dynamic interfaces that adapt to changing user contexts.

Usage Instructions

To use the RenderOnlyFor component in your React application, follow these steps:

  1. Once user has logged in get the user attributes from API and provide API response as a prop to RenderOnlyFor Component
  2. Once user switches across the pages get the page details from API and provide API response as pageDetails prop for RenderOnlyFor component .
  3. Wrap the RenderOnlyFor component in our component where dynamic rendering is needed.
  4. Add the appropriate action id to the elements in the components.