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
3
Maintainers
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
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:
- Once user has logged in get the user attributes from API and provide API response as a prop to RenderOnlyFor Component
- Once user switches across the pages get the page details from API and provide API response as pageDetails prop for RenderOnlyFor component .
- Wrap the RenderOnlyFor component in our component where dynamic rendering is needed.
- Add the appropriate action id to the elements in the components.