Directory structure
CMS React modules must be located in the/components/modules subdirectory of your CMS React project. The JavaScript file can either live within that directory or be contained within another directory, using the module name as the file/directory name.
/components/modules/MyModule/index.jsx
/components/modules/MyModule.jsx
MyModule/index.jsx or MyModule.jsx) must contain the following named exports:
Component: a React component to be rendered. It may contain islands.meta: a JavaScript object, equivalent to themeta.jsonfile in HubL modules.fields: a JSX tree using components from@hubspot/cms-components/fieldsto define module fields or a traditional JavaScript fields object.
index.jsx or ExampleModule.jsx file exports might look similar to the following:
index.jsx file below:
Module fields
Module fields can be expressed as a JSX tree using field components from@hubspot/cms-components/fields. These are the same module fields that HubL modules use. TypeScript definitions are included so that you can benefit from autocomplete and validation when defining fields. You can find all available fields in the fields reference documentation.
You may express field definitions as an array of JavaScript objects identical to the traditional HubL module JSON structure, exporting the same way as fields. However, using JSX syntax is recommended, as it comes with the benefits of improved readability, sharing field logic, and more.
Building fields with JSX
By writing module fields using JSX, you’ll benefit from improved readability, along with the ability to dynamically generate fields, share field logic between modules, and create custom abstractions around field definitions. For example, below is aFullNameField custom field component that abstracts out a group of two or three text fields:
fields export must be ModuleFields. Additionally, the above code makes use of FieldGroup, which is the component type that creates the field groups you would use in HubL modules.
In the FullNameField React component for the module fields defined above, props will have the following shape:
Repeated field groups
In addition toModuleFields and FieldGroup, another component type from @hubspot/cms-components/fields is RepeatedFieldGroup. It creates a field group repeater, and can be used as shown below:
Using field values
Field values are passed as props to theComponent export of the module. For example, the code below would use the field structure from the previous example inside your module’s component:
GraphQL
Like in HubL modules, you can bind a GraphQL data query to a React module to fetch data. The GraphQL integration currently supports querying data from HubDB and custom objects. Adding a namedquery export to a module will provide the query result to render in the component props as dataQueryResult. You can import and re-export a .graphql query file or a JavaScript expression that evaluates to a GraphQL query (e.g., with gql-query-builder).
ModuleComponent is shown below:
Passing HubL data
To automatically attach and pass through HubL context variables to your React modules, you can use thehublDataTemplate API.
First, export a string from your module via hublDataTemplate. In that string, set the hublData variable.
hublData.
cms-dev-server, update the URL to include /preview/ between the port number of your locally running server and the /module/... routes to have your local hublDataTemplate string resolve.
For example: hslocal.net:3000/preview/module/...