JSS components for rendering Sitecore fields in React applications
JSS for React provides components that help you render Sitecore fields in your React applications.
The following table shows the correspondence between Sitecore field types and JSS React components:
|
Sitecore field type |
JSS React component |
|---|---|
|
Date |
|
|
File |
|
|
Image |
|
|
General Link |
|
|
Single-Line Text, Multi-Line Text, or numeric types |
|
|
Rich Text |
|
The following is a list of supported components with examples of using them. In the examples, field data is provided by a Placeholder component.
Date
Date
The Date component helps render date and time content fields in JSS applications.
The Date component has the following properties:
|
Name |
Description |
Default |
|---|---|---|
|
|
Required. The field you want to render. Must be a Sitecore Date type. |
- |
|
|
The name of the HTML element wrapping the date value. |
|
|
|
Explicitly enable/disable inline editing. If |
|
|
|
A function that receives the field value as a parsed JavaScript Date object and returns the value to render in the wrapping |
- |
Usage
To use the field, you must import it into your component:
You can use the component as follows:
-
To render a simple date field:
-
To render a datetime field:
-
To render a date as a UTC-formatted string:
NoteThe
renderproperty is ignored in editing mode.The component provides direct access to the JS
Dateobject for formatting purposes. Therefore, you can use it to render localized date and datetime strings as follows:
File
File
The File component renders a file link.
The File component has the following properties:
|
Name |
Description |
|---|---|
|
|
Required. The route field you want to render. It represents a Sitecore File type with the properties |
|
|
A React Node, populating the rendered |
The File field does not support inline editing with the Experience Editor. However, it can be edited using the default field editor on components.
Usage
To use the field, you must import it into your component:
You can use the component as follows:
-
To render a simple file link:
-
To render a file link that opens in a new tab with custom anchor text:
Image
Image
The Image component allows you to render editable, responsive images.
The Image component has the following properties:
|
Name |
Description |
|---|---|
|
|
Required. Image field data. It accepts an |
|
|
A Boolean value you can use to enable or disable the inline editing of the image. Default: |
|
|
An array of |
|
|
An array of |
|
|
A custom regular expression that finds media URL prefix to be replaced by |
When using the imageParams or srcSet properties, your image parameters must be white-listed on the server for security purposes.
You can pass additional properties to the component. For example, you can specify values for the native image sizes attribute to enable responsive server-side rendered images.
Usage
To use the Image component, you must import it into your component:
You can use the Image component to render:
-
A simple image:
-
A responsive image:
-
An image with advanced configuration. For example, the following sample renders an image that is not editable in a Sitecore content and layout editor like the Experience Editor. The image is resized server-side based on the
imageParamsproperty:
Link
Link
The Link component helps you render the content of the General Link Sitecore field type.
The Link component has the following properties:
|
Name |
Description |
Default |
|---|---|---|
|
|
The route field you want to render. It must be a Sitecore General Link type. |
- |
|
|
Indicates whether to use the editor or Sitecore-formatted HTML output. Note If using editable output from Sitecore, Link creates a wrapper span around the Sitecore-provided markup and applies host element attributes to this span. |
|
|
|
A Boolean expression that enables or disables the display of a link description even when children exist. Note This setting is ignored in the Sitecore Experience Editor due to technical limitations, and the description is always rendered. |
Usage
To use the Link field helper component, you must import it into your component:
You can use the Link component to render:
-
External links:
-
Internal links, with HTML or other components:
-
Email links:
-
A content parameters link:
The link component accepts additional properties or attributes. For example:
In the Experience Editor, in editor mode, a link description is always rendered. If the link has any children, they are rendered as siblings to the link description.
Text
Text
The Text component helps you render Sitecore Single-Line Text, Multi-Line Text, or numeric fields.
The Text component has the following properties:
|
Name |
Description |
Default |
|---|---|---|
|
|
The field you want to render. |
- |
|
|
The name of the HTML element you want to wrap the text value. |
|
|
|
Indicates whether to use the editor or Sitecore-formatted HTML output. |
|
|
|
Enables or disables HTML encoding of the output value. A false value also implies |
|
For Multi-line Text, in the provided editable value, any line breaks are replaced by <br />. When using the non-editable value, you can process field.value to replace JSON newlines with the desired markup or use a CSS white-space value such as pre-wrap.
Usage
To use the Text component, you must import it into your component:
You can render:
-
A text field with default options:
-
A non-editable text field with a custom tag, CSS classes, and custom attributes:
If you want to render the raw value of a Sitecore text field, add the getFieldValue function to your import statement. You can then inspect the raw value in your application. For example:
RichText
RichText
The RichText component helps you render Sitecore Rich Text fields.
The RichText component has the following properties:
|
Name |
Description |
Default |
|---|---|---|
|
|
The field you want to render. |
- |
|
|
The name of the HTML element you want to wrap the text value. |
|
|
|
Indicates whether to use the editor or Sitecore-formatted HTML output. |
|
Usage
To use the RichText component, you must import it into your component:
You can render:
-
A field with default options:
-
A non-editable rich-text field with a custom tag, CSS classes, and custom attributes: