Logo

dev-resources.site

for different kinds of informations.

Two ways I discovered to pass dynamic parameters in (arrow and non-arrow) function.

Published at
5/27/2024
Categories
javascript
opensource
shadcn
clsx
Author
ramunarasinga
Categories
4 categories in total
javascript
open
opensource
open
shadcn
open
clsx
open
Author
13 person written this
ramunarasinga
open
Two ways I discovered to pass dynamic parameters in (arrow and non-arrow) function.

In my attempt to understand how the clsx utility is written by looking at its source code, I found that clsx does not have function parameters declared.

How is it possible to access the parameters without declaring them? clsx expects dynamic parameters. Below is an usage example from the docs

Then, I looked at the function code and from the lines below, it was apparent that through arguments variable, it is possible to access the function parameters.

export function clsx() {
 var i=0, tmp, x, str='', len=arguments.length;
Enter fullscreen mode Exit fullscreen mode

I searched on Google with this query “arguments without paramters in nodejs” and clicked on the first Stackoverflow link.

As per documentation:

arguments is an Array-like object accessible inside functions that contains the values of the arguments passed to that function.

The arguments object is a local variable available within all non-arrow functions.

First way of accepting dynamic function parameters (non-arrow):

The arguments object is useful for functions called with more arguments than they are formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments.

Using arguments only works in non-arrow functions.

Second way of accepting dynamic function parameters (arrow):

I read in some open source code sometime ago (I think, it is nextjs source code), don’t exactly remember but, you could use spread operator to accept dynamic function parameters. I found this stackoverflow question.

const childFunc = (str1, str2, str3) => \`${str1} ${str2} ${str3}\`;

const parentFunc = (...args) => childFunc('foo', ...args);

console.log(parentFunc('bar', 'fizz'));
Enter fullscreen mode Exit fullscreen mode

Since arguments variable does not work in arrow functions, I would choose a spread operator like shown in the above code snippet.

Conclusion:

Use arguments in non-arrow functions and a single parameter with a spread operator in arrow functions to accept dynamic function parameters.

Get free courses inspired by the best practices used in open source.

About me:

Website: https://ramunarasinga.com/

Linkedin: https://www.linkedin.com/in/ramu-narasinga-189361128/

Github: https://github.com/Ramu-Narasinga

Email: [email protected]

Learn the best practices used in open source.

References:

  1. https://github.com/lukeed/clsx/blob/master/src/index.js
  2. https://github.com/lukeed/uvu/blob/master/docs/cli.md
  3. https://stackoverflow.com/questions/59315539/why-can-arguments-be-passed-to-a-function-that-has-no-parameter-in-javascript
  4. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
  5. https://stackoverflow.com/questions/45217420/in-node-js-how-do-i-dynamically-pass-function-arguments-to-a-child-function
  6. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
shadcn Article's
30 articles in total
Favicon
Made FOSS for simplifying NextJS dev with OAuth And Postgres
Favicon
.gitkeep file in Shadcn/ui source code
Favicon
Introducing the Zod Schema Designer: A Visual Tool for Creating and Editing Zod Schemas
Favicon
Handling Local Storage for Toaster Notifications
Favicon
I created a visually pleasing HTML color viewer
Favicon
AISEKA Color Tool Website
Favicon
Color Palette is served with ShadCn Theme Editor
Favicon
Create React project with Vite and set up Tailwind, shadcn/ui
Favicon
Why Developers Love the Shadcn Sidebar Component
Favicon
A comparison of metadata configurations between Lobechat and Shadcn/ui
Favicon
Forms in seconds with ShardCn forms
Favicon
What is the Difference Between Radix UI and ShadCN?
Favicon
How Shadcn CLI uses error constants to improve code readability
Favicon
Exploring the ShadCN UI Library: A Comprehensive Guide
Favicon
10 Essential Shadcn Components Every Developer Should Know About
Favicon
The beginning of my open source journey
Favicon
Install ShadCN and Tailwind in Vite React Project
Favicon
How to add shadcn to existing project
Favicon
How to Use Shadcn UI with a React Project
Favicon
How to Install Shadcn with Next.js 14: A Step-by-Step Guide
Favicon
Shadcn Date Picker with custom implementation
Favicon
How to Fix Shadcn UI Adding Wrong Folder for Components
Favicon
Material UI vs Shadcn
Favicon
Building UIs with Franken UI, a Shadcn alternative
Favicon
Implement ShadCn form with Validation
Favicon
Top 5 Coolest shadcn/ui Extensions
Favicon
Shadcn UI: Must-Have Tools & Resources
Favicon
shadcn-ui/ui codebase analysis: examples route explained.
Favicon
Two ways I discovered to pass dynamic parameters in (arrow and non-arrow) function.
Favicon
Creating a User Nav bar in Next.js 14 using Shadcn UI and Tailwind CSS

Featured ones: