Purpose
Detail the steps to create a basic “Hello World” component and define a content source for your page.
Purpose
Detail the steps to create a basic “Hello World” component and define a content source for your page.
Audience
Developers learning to work with Arc XP.
To create and publish a component that displays Hello World on a newly created page, take the following steps:
Before creating a new component, install a local instance of PageBuilder Engine. See Running PageBuilder Locally for details on installing and running a local instance.
To create a new component in PageBuilder Engine, take the following steps:
Open your project folder in your preferred code editor.
Under the components/features
folder, create a Greet
component folder.
Inside the Greet
folder, create a default.jsx
file.
Paste the following code inside the default.jsx
file:
import React from 'react';import PropTypes from '@arc-fusion/prop-types';
const Greet = ({ customFields }) => (<div className="hello_text"> Hello {customFields.name || 'User'} !</div>);
Greet.propTypes = {customFields: PropTypes.shape({ name: PropTypes.string.tag({ label: 'Name', description: 'What is your name?', }),}),};
export default Greet;
Save the default.jsx
file. This component defines the custom field name
as a prop.
Build and start the local PageBuilder Engine server with the following command in the terminal:
npx fusion start
This process can take several minutes.
Go to http://localhost/pagebuilder/pages to access the PageBuilder editor.
To create a new page in the PageBuilder Editor (without Arc XP credentials) you must first create a template and save it as a page. Take the following steps:
On the top navigation menu, click the Templates tab.
Click the Create Template button.
Type a name for the Template.
Click the Create button. The PageBuilder Editor opens.
On the left sidebar, click the Curate button.
Scroll down to the Save as Page section.
Fill out the following fields:
Click the Save Page button.
Layouts enable you to customize your page. For example, the number of columns, the position of sections, and the page’s distribution on the screen. You can render the page layout in any combination you create. Your editors then use the Editor UI to populate the page with content according to the layout structure.
To customize layouts in PageBuilder Engine, take the following steps:
On the top navigation menu, click the Pages tab.
Click the name of the page you created in Creating a New Page. The PageBuilder Editor opens.
On the left sidebar, click the Curate button.
Click the + Add Block button.
The UI displays a middle panel showing the list of all available blocks.
Select the Greet default block.
HelloUser!
according to the default value for the name
custom field.Customize the value of the name
custom field.
The value contained in name
field changes the message displayed in the right panel. For example, if the value is “John” the message in the right panel changes to HelloJohn!
.
After finishing your changes, take the following steps to publish your page:
On the left sidebar, click the Publish button.
The left panel now shows Draft Options.
Click the Share & Publish button.
The page becomes accessible on the local server.
You can access your production URLs in the PageBuilder UI under the Pages section, which lists your created pages. Select a page URI, and the UI loads the page in render mode.
Alternatively, enter the page’s URI directly in your browser, as shown in the following example:
http://localhost/pf/test/
This section walks you through creating a component that queries data from a public API and renders it as a table.
The following example is based on the Facts endpoint from the Dog API, a public API that does not require any authentication and allows Cross-Origin Resource Sharing (CORS).
To create the component, take the following steps:
Under the components/features
folder, create a Dogs
component folder.
Inside the Dogs
folder, create a default.jsx
file.
Paste the following code inside the default.jsx
file:
import React, { useEffect, useState } from 'react';import PropTypes from '@arc-fusion/prop-types';
const DogFactsTable = ({ customFields }) => { const [dogFacts, setDogFacts] = useState([]);
useEffect(() => { const fetchDogFacts = async () => { try { const response = await fetch( `https://dogapi.dog/api/v2/facts?limit=${customFields.factsLimit} || '1'` ); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); setDogFacts(data.data || []); } catch (error) { console.error("Error fetching dog facts:", error); } };
fetchDogFacts(); }, [customFields.factsLimit]);
return ( <div style={{ padding: "20px", fontFamily: "Arial, sans-serif" }}> <h1 style={{ textAlign: "center", color: "#4CAF50" }}>Dog Facts</h1> <table style={{ width: "100%", borderCollapse: "collapse", boxShadow: "0 4px 8px rgba(0, 0, 0, 0.1)", }} > <thead> <tr style={{ backgroundColor: "#4CAF50", color: "white", textAlign: "left", }} > <th style={{ padding: "10px" }}>ID</th> <th style={{ padding: "10px" }}>Type</th> <th style={{ padding: "10px" }}>Fact</th> </tr> </thead> <tbody> {dogFacts.length > 0 ? ( dogFacts.map((fact) => ( <tr key={fact.id} style={{ backgroundColor: "#f9f9f9", borderBottom: "1px solid #ddd", }} > <td style={{ padding: "10px" }}>{fact.id}</td> <td style={{ padding: "10px" }}>{fact.type}</td> <td style={{ padding: "10px" }}>{fact.attributes.body}</td> </tr> )) ) : ( <tr> <td colSpan="3" style={{ textAlign: "center", padding: "20px", backgroundColor: "#ffdddd", }} > Loading dog facts... </td> </tr> )} </tbody> </table> </div> );};
DogFactsTable.propTypes = { customFields: PropTypes.shape({ factsLimit: PropTypes.string.tag({ label: 'factsLimit', description: 'Specify number of facts to display', }), }),};
export default DogFactsTable;
The Dogs component comprises the following core elements:
fetch
function that queries the Facts endpoint with a value for the available limit
parameter.factsLimit
custom field to pass as a prop to the fetch
function.Save the default.jsx
file. The component becomes available in the Curate section.
Add the component to your page.
Modify the limitFacts
custom field in the Block details panel to control the number of dog facts that the component displays on the table (possible values are 1-5).