index.ts
files as well in following Steps of it.
In Step 1 of the ‘How to create widget’s section’s Step: 1’, section, you’ve observed that we’ve created a component and ensured that the necessary changes are made in the respective index.ts
files as well, as outlined in the subsequent steps.
When you made your Widget, you likely created a folder for the component. In this folder, you have files like a .tsx file which returns JSX, where you define how the component looks and an index.ts file that goes along with it.
exp-test-component.tsx
, it might vary based on your requirements. The file name could also be different depending on the specific component you’re creating.
The component file may appear similar to the snippet below:
exp-test-component.tsx
Default
id
as a prop
id
. This prop is automatically provided to each component integrated with the widget. You can directly destructure it from the component’s props.exp-test-component-controller.tsx
To implement all the business logic for the component, we’ll create a component-controller file, responsible for managing all the business logic. In the context of developing a React component, it’s important to centralize all state management, including useState
, useEffect
, and other state manipulations, within this file exclusively. The exp-test-component.tsx
will solely focus on rendering JSX based on the state managed in the controller file.
So, at this point, we’ll retrieve the data of the selected Content Model record. The selection is made from the pop-up of the trait type exp_contentModalPopUp. This trait returns data in terms of the selected record’s id
, from which we’ll fetch the record’s data by making an API call in the controller file.
So, basic struture look’s like for the each controller file as below snippet.
id
, modelInternalName
, and modelKeyForSSR
as arguments.
As a result, it furnishes three essential components crucial for component implementation: componentDataDispatcher
, setComponentDataDispatcher
, and isComponentLoaded
.
Here’s a breakdown of the provided elements:
componentDataDispatcher
: This object contains two values: {isLoading, componentData}
.
true/false
) indicating whether the component’s data is being fetched or not. Different states utilized within the useEffect
update the value of this state.getContentLibraryData
’.setComponentDataDispatcher
: This serves as a setter method for the state, similar to when using useState
. You specify an object containing type and state values, which are then set by the reducer.
isComponentLoaded
: This state holds a boolean value (true/false
), responsible for indicating whether the component’s data has already been fetched or not.
componentDataDispatcher
, which contains the isLoading
and componentData
state values.
You can utilize the componentDataDispatcher
’s isLoading
, componentData
, and contentModel
to display the loader of the component or a message indicating to “Select a record”.
All the data required for the component will reside in the componentDataDispatcher
’s componentData
key. You can utilize this data to render the component and return JSX accordingly.
With Experro Storefront, integrating a component with a widget is a breeze, and customizing it to your exact specifications couldn’t be simpler.