Using React components in Sitecore
PROBLEM BACKGROUND
Do you need to incorporate a React component into Sitecore? Is your team using SXA or a similar technology which provides out of the box components that your building your site with? Are you NOT using Sitecore JSS / headless yet?
We are developing a new site in Sitecore 10. We wanted any component that interacts with our CRM data to be written in React. We are NOT using Sitecore JSS but we are using Sitecore SXA.
The question becomes, how can we incorporate a new Sitecore rendering that will use React as the front end technlogy. How will we get the rendering and datasource parameters successfully passed down to the React component?
One approach I've seen on several Sitecore sites, blog posts, etc is to use ReactJS.Net. That solution works but feels like overkill to include a React component.
- You get to maintain ReactJS.Net components in your solution,
- You need to have javascript engines on your server and in your local solution to allow for server compiliation of your React components.
- You need to insert ReactJS.Net into the Sitecore pipeline.
- Create your React component.
- Create a function that can instantiate your React component.and add this function to the window object. Putting the function under an object will avoid collisions with other components.
STEP 2: In your razor view, instanstiate the React component, passing rendering and datasource parameters to the React component.
Here is an example of what this looks like:
Notice in the example razor view, I added a delay when loading the React component in the EE. This was because the React component would not load until all EE JS events and functionality was loaded. Time permitting I will go back and resolve this issue in a more elegant fashion.
This solution is working well for us. It avoids the overhead of incorporating ReactJS.Net into the Sitecore pipeline.
Comments
Post a Comment