{"id":57791,"date":"2023-07-28T16:49:35","date_gmt":"2023-07-28T11:19:35","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57791"},"modified":"2024-06-10T15:38:18","modified_gmt":"2024-06-10T10:08:18","slug":"building-custom-components-in-aem-spa-editor-with-reactjs-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/building-custom-components-in-aem-spa-editor-with-reactjs-a-step-by-step-guide\/","title":{"rendered":"Building Custom Components in AEM SPA Editor with ReactJS: A Step-by-Step Guide"},"content":{"rendered":"<h2><b>Introduction<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Adobe Experience Manager (AEM) is a comprehensive content management solution for building websites, mobile apps, and forms. It makes it easy to manage your marketing content and assets. One of the powerful features of AEM is the Single Page Application (SPA) Editor, which allows developers to create more fluid and responsive user experiences. In this blog, we will walk through the process of creating a custom component in AEM SPA Editor using ReactJS.<\/span><\/p>\n<h2><b>Prerequisites<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Before we start, make sure you have the following:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">A running instance of AEM 6.4 or later.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Basic understanding of AEM, ReactJS, and JavaScript.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Node.js and npm installed on your machine.<\/span><\/li>\n<\/ol>\n<p><b>Step 1: Setting Up Your Project<\/b><\/p>\n<p><span style=\"font-weight: 400;\">First, we need to set up our AEM project. You can use the AEM Project Archetype to generate a new project. Run the following command in your terminal:<\/span><\/p>\n<p><b><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57785 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.31-AM-1024x277.png\" alt=\"\" width=\"625\" height=\"169\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.31-AM-1024x277.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.31-AM-300x81.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.31-AM-768x208.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.31-AM-624x169.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.31-AM.png 1442w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/b><\/p>\n<p><span style=\"font-weight: 400;\">This will create a new AEM project with a React frontend module.<\/span><\/p>\n<p><b>Step 2: Creating the Dialog<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In your project&#8217;s core module, navigate to the components directory and create a new directory for your component. Inside this directory, create a file named _cq_dialog\/.content.xml and define the dialog for your component. Here&#8217;s a simple example:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57786 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.44-AM-1024x740.png\" alt=\"\" width=\"625\" height=\"452\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.44-AM-1024x740.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.44-AM-300x217.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.44-AM-768x555.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.44-AM-624x451.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.52.44-AM.png 1386w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><b>Step 3: Creating the Java Model<\/b><\/p>\n<p><span style=\"font-weight: 400;\">In the core module of your project, navigate to src\/main\/java\/com\/mycompany\/myspa\/core\/models and create a new Java class named CustomBanner.java. This class will serve as the Sling Model for your component. Here&#8217;s a simple example:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57790 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.56.32-AM-1024x674.png\" alt=\"\" width=\"625\" height=\"411\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.56.32-AM-1024x674.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.56.32-AM-300x197.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.56.32-AM-768x506.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.56.32-AM-624x411.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.56.32-AM.png 1358w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">To ensure the Sling Model is automatically recognized by the JSON model API, it&#8217;s necessary to implement the ComponentExporter interface.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You need to modify the RESOURCE_TYPE static variable to refer to the AEM component wknd-spa-react\/components\/custom-banner created in the previous step. The resource type of the component is the key that links the Sling Model to the AEM component and eventually maps it to the React component.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Incorporate the getExportedType() method into your component. This method reveals the resource type, allowing the mapping to the React component.<\/span><\/p>\n<p><b>Step 4: Creating the React Component and Mapping it with AEM Component<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Navigate to the components directory in your project. Create a new directory for your custom component, and inside this directory, create file: customBanner.js<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57787 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.05-AM-1024x909.png\" alt=\"\" width=\"625\" height=\"555\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.05-AM-1024x909.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.05-AM-300x266.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.05-AM-768x682.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.05-AM-624x554.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.05-AM.png 1298w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">In this example, the MapTo function from the <\/span><b>@adobe\/aem-react-editable-components <\/b><span style=\"font-weight: 400;\">package is used to map the React component to the corresponding AEM component in the SPA Editor. The first argument of MapTo is the React component itself, and the second argument is the resource type of the AEM component.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">By wrapping your React component with the MapTo function and specifying the resource type, the SPA Editor can recognize and render your component in the AEM authoring environment.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Finally, navigate to the src directory in your project&#8217;s spa module and open the import-components.js file. Add the following lines:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57788 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.14-AM-1024x148.png\" alt=\"\" width=\"625\" height=\"90\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.14-AM-1024x148.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.14-AM-300x43.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.14-AM-768x111.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.14-AM-624x90.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.14-AM.png 1356w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><b>Step 5: Deploying the project<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Deploy all of the updates to a local AEM environment from the root of the project directory using Maven commands.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57789 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.21-AM-1024x91.png\" alt=\"\" width=\"625\" height=\"56\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.21-AM-1024x91.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.21-AM-300x27.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.21-AM-768x68.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.21-AM-624x56.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/Screenshot-2023-07-17-at-11.53.21-AM.png 1368w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Congratulations<span style=\"font-weight: 400;\">! You&#8217;ve successfully created a custom component to be used with the SPA Editor using ReactJS. Now, you can use this component in your AEM pages to create more interactive and personalized user experiences. The SPA Editor and ReactJS together provide a powerful toolset for creating dynamic and responsive UIs in your AEM projects. Happy coding!<\/span><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction Adobe Experience Manager (AEM) is a comprehensive content management solution for building websites, mobile apps, and forms. It makes it easy to manage your marketing content and assets. One of the powerful features of AEM is the Single Page Application (SPA) Editor, which allows developers to create more fluid and responsive user experiences. In [&hellip;]<\/p>\n","protected":false},"author":1605,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":501},"categories":[5868,446,3038,1994,1,4488],"tags":[4847,5287,5290,2046,5289,2151,5291],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57791"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1605"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57791"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57791\/revisions"}],"predecessor-version":[{"id":57889,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57791\/revisions\/57889"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}