Learn more about CMS React modules, including how to structure your files and use fields.
/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/ExampleModule/index.jsx
/components/modules/ExampleModule.jsx
ExampleModule/index.jsx
or ExampleModule.jsx
) must contain the following named exports:
Component
: a React component to be rendered. It may contain islands.meta
: a JavaScript object, equivalent to the meta.json
file in HubL modules.fields
: a JSX tree using components from @hubspot/cms-components/fields
to 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:
@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.
FullNameField
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:
ModuleFields
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:
Component
export of the module. For example, the code below would use the field structure from the previous example inside your module’s component:
query
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:
hublDataTemplate
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/...