From the course: Master React 19, Build Ecommerce Solutions, and Prepare for Interviews with TypeScript, Next.js, and Remix

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Parameter passing

Parameter passing

In certain situations, we might have to pass parameters to the event handler function and there is a specific way of doing it in react. Let's practically see it. So here let me pass a string as a parameter to the handle click function. Here I will pass hello world as parameter. Now we will receive it in the handleClick function and will display the value of the parameter. Let's save the code and check. Here you can observe that the value of the parameter is rendered in the console. It means the function is called without clicking the button. This is because if we use parentheses right after the function in the JSX, then the compiler calls the function while rendering the JSX. This is an unexpected behavior occurred during the compilation. To prevent this issue, we have to return the handleClick function by using the arrow function in the onClick property and then we can pass the parameter to the function. So, let me add the arrow function to the onClick property and return the…

Contents