Walkthrough: Setting up the React project
This walkthrough is the part one of the Rapid development using Search UI components walkthrough series.
You will learn how to:
-
Install JS SDK and UI Components for React.
-
Retrieve credentials.
-
Authenticate with credentials.
Before you begin
We recommend you start with making an inventory of the widgets that you need to create. In the Customer Engagement Console (CEC), the Widgets section lists all widgets that have been configured for your implementation. The listing can have widgets that are not used in your application.
We strongly recommend you familiarize yourself with the entity or entities configured in the CEC.
To view details of all entities in the implementation:
-
Log in to the CEC using your Search credentials.
-
On the menu bar, click Administration then click Domain settings.
-
On the General Settings tab, click Entities. You can find the names, and attributes of all the entities in your implementation.
-
On the Attributes tab, from the Entity dropdown, for a list of attributes, select an entity.
To inventory widgets and pages:
-
Log in to the CEC using your Search credentials.
-
On the menu bar, to view lists of widgets, click Widgets.
-
On the menu bar, to list the pages you need to create, click Pages.
-
In the Widgets tab, from the table, for every widget, retrieve the
name
,rfk_id
,type
,status
, and the page or pages it is hosted.
For successful rendering of widget components, make sure that widget components have the same rfk_id
and type
as configured in the CEC; and widgets are published and have Active status.
Install JS SDK and UI Components for React
Developers can install Sitecore Search JS SDK and UI Components packages into their projects. In your React code, you can import classes, functions, and objects from these packages, just as you would from other JS packages.
To install the latest JS SDK and UI Components for React:
-
To add Search JS SDK for React to your project, run the following in your project:
RequestResponsenpm install --save @sitecore-search/react
-
To add Search UI Components for React to your project, run the following in your project:
RequestResponsenpm install --save @sitecore-search/ui
-
In the
Root
component of your React project, import theWidgetsProvider
component as shown in the following:RequestResponseimport { WidgetsProvider } from "@sitecore-search/react"; const Root = () => { return ( <WidgetsProvider> </WidgetsProvider> )};
Retrieve credentials
To protect your privacy and content, we require that your application authenticate with Search before making any requests. You need to use the credentials associated with your account and domain.
To retrieve your Search credentials:
-
Log in to the Customer Engagement Console (CEC) with your personal credentials.
-
On the menu bar, click Developer Resources.
-
In the API Keys pane on the right, copy the key for data requests.
Authenticate with credentials
To protect your privacy and content, we require that your application authenticate with Search before making any requests. You need to use the credentials associated with your account and domain. You can choose to authenticate before you make any data requests or along with your fist data request.
The JS SDK sets a cookie and includes the UUID when tracking events. Visitor information, UUID, is required to personalize search results.
After the JS SDK successfully authenticates the application, the JS SDK includes the necessary authentication information in all future requests.
We recommend you include the credentials in the Root
or App
component of your React application.
To authenticate when your subdomain is set up:
-
In the
Root
component of your React project, add theWidgetsProvider
component with the correct credentials for authentication, as shown in the following code block.RequestResponseimport { WidgetsProvider } from "@sitecore-search/react"; const Root = () => { return ( <WidgetsProvider env='<environment>' customerKey='<customer key>' serviceHost='<api domain>' > <MyApp /> </WidgetsProvider> )};
The credentials you use depend on how your domain is set up for Sitecore Search. For instructions on authenticating when your subdomain is not set up with Search, refer to Authenticate without domain set up.
For the env
attribute, the value must be one of apse2
(for Asia/Oceania domains), prodEu
(for European domains), prod
(for US domains), or staging
(for US domains).