{"id":2270,"date":"2019-10-15T01:55:15","date_gmt":"2019-10-15T01:55:15","guid":{"rendered":"http:\/\/upmostlymulti.onpressidium.com\/?p=2270"},"modified":"2021-10-28T11:01:18","modified_gmt":"2021-10-28T16:01:18","slug":"pass-a-parameter-through-onclick-in-react","status":"publish","type":"post","link":"http:\/\/upmostly.com\/tutorials\/pass-a-parameter-through-onclick-in-react","title":{"rendered":"Pass a Parameter Through onClick in React"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1160\" height=\"600\" src=\"http:\/\/upmostly.com\/wp-content\/uploads\/pass-parameter-through-onclick-react.jpg\" alt=\"\" class=\"wp-image-2288\" srcset=\"https:\/\/upmostly.com\/wp-content\/uploads\/pass-parameter-through-onclick-react.jpg 1160w, https:\/\/upmostly.com\/wp-content\/uploads\/pass-parameter-through-onclick-react-300x155.jpg 300w, https:\/\/upmostly.com\/wp-content\/uploads\/pass-parameter-through-onclick-react-768x397.jpg 768w, https:\/\/upmostly.com\/wp-content\/uploads\/pass-parameter-through-onclick-react-1024x530.jpg 1024w\" sizes=\"auto, (max-width: 1160px) 100vw, 1160px\" \/><\/figure>\n\n\n\n<p>A common thing you will want to learn in React is how to pass a value as a parameter through the <strong>onClick<\/strong> event handler. Read on to learn how!<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">App.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> React <span class=\"token keyword\">from<\/span> <span class=\"token string\">'react'<\/span><span class=\"token punctuation\">;<\/span>\n\n<span class=\"token keyword\">const<\/span> <span class=\"token function-variable function\">ExampleComponent<\/span> <span class=\"token operator\">=<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">=><\/span> <span class=\"token punctuation\">{<\/span>\n  \n  <span class=\"token keyword\">function<\/span> <span class=\"token function\">sayHello<\/span><span class=\"token punctuation\">(<\/span>name<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\n    <span class=\"token function\">alert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token template-string\"><span class=\"token string\">`hello, <\/span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${<\/span>name<span class=\"token interpolation-punctuation punctuation\">}<\/span><\/span><span class=\"token string\">`<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n  <span class=\"token punctuation\">}<\/span>\n  \n  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>button<\/span> <span class=\"token attr-name\">onClick<\/span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">=><\/span> <span class=\"token function\">sayHello<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'James'<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token punctuation\">><\/span><\/span><span class=\"token plain-text\">Greet<\/span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;\/<\/span>button<\/span><span class=\"token punctuation\">><\/span><\/span>\n  <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n<span class=\"token punctuation\">}<\/span>\n\n<span class=\"token keyword\">export<\/span> <span class=\"token keyword\">default<\/span> ExampleComponent<span class=\"token punctuation\">;<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>For those who want the TL;DR and go straight to the code, take a look at the example above \u261d\ufe0f.<\/p><\/blockquote>\n\n\n\n<p><strong>ExampleComponent<\/strong> is a very simple React component that contains one button and one function.<\/p>\n\n\n\n<p>To understand how to pass a value as a parameter through an <a href=\"http:\/\/upmostly.com\/tutorials\/react-onclick-event-handling-with-examples\">onClick event handler<\/a>, take a look at the line of code inside of the return statement. It&#8217;s a single button with one event handler: <strong><em>onClick<\/em><\/strong>.<\/p>\n\n\n\n<p>Typically, to call a function when we click a button in React, we would simply pass in the name of the function to the <strong>onClick<\/strong> handler, like so:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">Calling a function without a parameter<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token operator\">...<\/span>\n  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>button<\/span> <span class=\"token attr-name\">onClick<\/span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=<\/span><span class=\"token punctuation\">{<\/span>sayHello<span class=\"token punctuation\">}<\/span><\/span><span class=\"token punctuation\">><\/span><\/span><span class=\"token plain-text\">Greet<\/span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;\/<\/span>button<\/span><span class=\"token punctuation\">><\/span><\/span>\n  <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n<span class=\"token operator\">...<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>Notice how in the <strong>ExampleComponent<\/strong> code above, we pass in more than just the function name.<\/p>\n\n\n\n<p>In order to pass a value as a parameter through the <strong>onClick<\/strong> handler we pass in an arrow function which returns a call to the <strong><em>sayHello<\/em><\/strong> function.<\/p>\n\n\n\n<p>In our example, that argument is a string: &#8216;<em>James<\/em>&#8216;:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><div><span class=\"token operator\">...<\/span>\n  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\n    <span class=\"token operator\">&lt;<\/span>button onClick<span class=\"token operator\">=<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">=><\/span> <span class=\"token function\">sayHello<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'James'<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><span class=\"token operator\">><\/span>Greet<span class=\"token operator\">&lt;<\/span><span class=\"token operator\">\/<\/span>button<span class=\"token operator\">><\/span>\n  <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n<span class=\"token operator\">...<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>It&#8217;s this trick of writing an inline arrow function inside of the <strong>onClick<\/strong> handler which allows us to pass in values as a parameter in React.<\/p>\n\n\n\n<p>Let&#8217;s explore some more common examples!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pass a Button&#8217;s Value as a Parameter Through the onClick Event Handler<\/h2>\n\n\n\n<p>You might want to pass in the value or name of the button or input element through the event handler.<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><div><span class=\"token operator\">...<\/span>\n  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\n    <span class=\"token operator\">&lt;<\/span>button value<span class=\"token operator\">=<\/span><span class=\"token string\">\"blue\"<\/span> onClick<span class=\"token operator\">=<\/span><span class=\"token punctuation\">{<\/span>e <span class=\"token operator\">=><\/span> <span class=\"token function\">changeColor<\/span><span class=\"token punctuation\">(<\/span>e<span class=\"token punctuation\">.<\/span>target<span class=\"token punctuation\">.<\/span>value<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">}<\/span><span class=\"token operator\">><\/span>Color Change<span class=\"token operator\">&lt;<\/span><span class=\"token operator\">\/<\/span>button<span class=\"token operator\">><\/span>\n  <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n<span class=\"token operator\">...<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>The button now has an additional attribute, named value. We can get access to the button&#8217;s attributes in its event handlers by accessing the event through the event handler.<\/p>\n\n\n\n<p>The example above shows the variable <strong><em>e<\/em> <\/strong>that is provided from the onClick event handler. This stands for <em><strong>event<\/strong><\/em>. Once we have the event, we can access values such as the value or name attribute.<\/p>\n\n\n\n<p>To learn more about the <strong>onClick<\/strong> event handler read my tutorial on the <a href=\"http:\/\/upmostly.com\/tutorials\/react-onclick-event-handling-with-examples\">onClick Event Handler (With Examples)<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common thing you will want to learn in React is how to pass a value as a parameter through the onClick event handler. Read on to learn how!<\/p>\n","protected":false},"author":8,"featured_media":2288,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[33,4,39],"class_list":["post-2270","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-beginner-react-tutorials","tag-react","tag-react-event-handlers"],"acf":[],"_links":{"self":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/2270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/comments?post=2270"}],"version-history":[{"count":2,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/2270\/revisions"}],"predecessor-version":[{"id":3539,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/2270\/revisions\/3539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media\/2288"}],"wp:attachment":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media?parent=2270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/categories?post=2270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/tags?post=2270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}