Setting up the JS SDK in your React project
Before you can begin integrating your React application with Sitecore Search using the JS SDK, you need to install and set up the JS SDK in your React project. The various procedures in this walkthrough cover installation of Search packages and authentication.
This walkthrough describes how to:
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
andPageController
components.RequestResponseimport { PageController, WidgetsProvider } from "@sitecore-search/react"; const Root = () => { PageController.getContext().setLocaleLanguage('en'); PageController.getContext().setLocaleCountry('us'); return ( <WidgetsProvider> </WidgetsProvider> )};
NoteIf you don't have multiple locales configured, you can remove the
PageController
component and the two lines before the return statement. The default locale is automatically used in this case.
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 Sitecore Search with your Sitecore Search 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 the WidgetsProvider
component with the correct credentials for authentication and the PageController
component to set the locale, as shown in the following code block.
import { PageController, WidgetsProvider } from "@sitecore-search/react";
const Root = () => {
PageController.getContext().setLocaleLanguage('en');
PageController.getContext().setLocaleCountry('us');
return (
<WidgetsProvider
env='<environment>'
customerKey='<customer key>'
serviceHost='<api domain>'
>
<MyApp />
</WidgetsProvider>
)};
If you don't have multiple locales configured, you can remove the PageController
component and the two lines before the return statement. The default locale is automatically used in this case.
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).