Build your first component

Timing 20 minutes

This is part four of the getting started walkthrough series. Before you can complete the steps here, make sure you have created a project and environment, created a site and page, and set up your local development environment.

In this example, you'll be creating a React Next.JS component in XM Cloud. You'll be creating a component that includes a title and text. To create the component, you will clone one of the existing components that are included out of the box in XM Cloud. Cloning an existing component saves development time by creating the boilerplate items and configurations that your component requires, and makes sure that the required base templates are configured to support the content authoring features in Pages.

This walkthrough describes how to:

Create and deploy the new component to XM Cloud

In the first step, you create the component structure in your local Next.JS project. To do this:

  1. Ensure you still have the cloned repository open in your code editor.

  2. In the /headapps/nextjs-starter/src/components folder create a new file and name it TitleAndText.tsx.

  3. Paste the following code into the new file:

    RequestResponse
    import React from 'react';
    import { Field, Text } from '@sitecore-jss/sitecore-jss-nextjs';
    
    interface Fields {
      Title: Field<string>;
      Text: Field<string>;
    }
    
    export type TitleAndTextProps = {
      params: { [key: string]: string };
      fields: Fields;
    };
    
    export const Default = (props: TitleAndTextProps): JSX.Element => {
      return (
        <div className="container-default">
          <h1 className="component title row">
            <Text field={props.fields.Title} />
          </h1>
          <div className="component text row">
            <Text field={props.fields.Text} />
          </div>
        </div>
      );
    };
     
    
  4. Save the file.

  5. Add the change to a commit and push the change to your repo. This will automatically kick off a deployment to XM Cloud. You can follow its progress in the Deploy app. Proceed to the next step after the deployment is complete.

Create the rendering definition

Now, you proceed to create the rendering definition for the component. Rendering definitions are used to tell XM Cloud where in the project the component's .tsx file is, and what data it needs to render correctly. You'll do this by cloning an existing rendering, thereby automatically generating all the required items, instead of having to do it manually. Then, you'll add the rendering to the headless module that was created when you created the site collection. You can find the module in /sitecore/system/Settings/Project.

To clone the component:

  1. In the Cloud Portal, click the tile for the environment you created earlier - XMC Getting Started.

  2. In the pane on the right, click Open app.

  3. On the top menu bar, click Content, then click Content Editor.

  4. In the Content Editor, navigate to /sitecore/layout/Renderings/Feature/Headless Experience Accelerator/Page Content. Right-click the rendering named Rich Text and click Scripts > Clone Rendering.

  5. In the Create derivative rendering window, specify the following:

    • On the General tab, enter the name TitleAndText.

    • On the General tab, in the Add to module field, select Renderings/Project/XMC Getting Started/XMC Getting Started.

    • On the Parameters tab, select Make a copy of original rendering parameters. This decouples the rendering from the original component and reduced dependencies.

    • On the Datasource tab, in the Datasource field, select Make a copy of original datasource.

    The Create derivative rendering window used when cloning the Rich Text rendering in the Content Editor
  6. Click Proceed, and close the window when the process is finished.

  7. Navigate to /sitecore/templates/Project/XMC Getting Started/Getting Started Content/Text, and ensure the Builder tab is selected.

  8. Change the values for the fields you created:

    • Title - select Single-Line Text.

    • Text - select Multi-Line Text.

    Defining the fields in the new title and text component in the Content Editor
  9. Make sure the Source column is empty for both fields.

  10. In the top-left corner, click Save.

    Save icon in Content Editor
  11. Navigate to /sitecore/content/XMC Getting Started/XMC Getting Started/Presentation/Available Renderings.

  12. Right-click the Available Renderings item and click Insert > Available Renderings.

  13. Name the new item Getting Started Content and click OK. The new item opens automatically.

  14. In the Renderings field, click Edit.

  15. In the tree on the left, locate and select the rendering item you created previously at /Layout/Renderings/Project/XMC Getting Started/Getting Started Content/TitleAndText. Click the arrow to move it to the list on the right, then click OK.

    Edit the rendering to include the new title and text component
  16. In the top-left corner, click Save.

  17. Navigate back to the main XM Cloud dashboard by clicking the 9-square icon in the top left.

Add the component to your site page

At this stage, you've completed the required configuration for the new component and it's available to use on your site. In this step, you can return to Pages and add the component to the page you created earlier, after which you'll be able to see it on your local development instance.

  1. On the main XM Cloud Sites dashboard, click the tile of the new site to open Pages, or, in a new browser tab, open https://pages.sitecorecloud.io/.

  2. On the Components tab, in the Getting Started Content section, you can now see the Title and Text component. Drag it onto one of the plus icons on the page. You should now see the component rendered on the page, with a No text in field indicator for the title and text fields.

  3. Type in some text to the title and text fields.

  4. In the previous walkthrough, you connected your application to the local host. Check if it's still running, and if it isn't, in the terminal window, run npm run start:connected.

  5. In your browser, navigate to http://localhost:3000/ to see the new component rendered with the text you entered.

You've completed the walkthrough series! You can now continue to create new components, test your changes locally, and push to your source control provider - this kicks off a deployment in XM Cloud and makes new components available in Pages.

Do you have some feedback for us?

If you have suggestions for improving this article,