Skip to content

A ReactJS module for building Sitecore components with React

License

Notifications You must be signed in to change notification settings

mknayak/sitecore.react

 
 

Repository files navigation

Sitecore.React

NuGet version

A ReactJS module for building Sitecore components with React and ReactJS.NET.

The module has 2 parts. A Sitecore package that installs the required JsxControllerRendering and JxsViewRendering templates, and the Nuget package for use in your projects. This adds the required pipeline processors and components to render the React Jsx files as Sitecore renderings.

Features

  • On the fly Jsx to JavaScript compilation via Babel and ReactJS.NET
  • Server-side component rendering. Initial renders are super-fast and great for SEO
  • Full support for datasources, personalization and testing

Getting Started

  • Install the NuGet package
Install-Package Sitecore.React
  • Install the Sitecore package Create your JsxControllerRendering controller and action
public SampleReactController : Controller 
{
	public ActionResult SampleReactRendering() 
	{
		var data = new {
			Title = FieldRenderer.Render(Sitecore.Context.Item, "Title"),
			Text = FieldRenderer.Render(Sitecore.Context.Item, "Text")
		};

		return this.React("~/views/SampleReact/SampleReactRendering.jsx", data);
	}
}
  • Create your Jsx component

It's requried to have Components_ prefix as in the code component it created with name Components__{ComponentName}

var Components_SampleReactRendering = React.createClass({
    render: function() {
        return (
            <div>
                <h1 dangerouslySetInnerHTML={{__html: this.props.data.Title}}></h1>
                <div dangerouslySetInnerHTML={{__html: this.props.data.Text}}></div>
            </div>
        );
    }
});
  • Create the rendering item in Sitecore and assign to an items presentation
  • Add the React JavaScript links and the Jsx bundle to your main layout
  <script src="//fb.me/react-15.0.1.js"></script>
  <script src="//fb.me/react-dom-15.0.1.js"></script>
  @Scripts.Render(Sitecore.React.Configuration.Settings.ReactBundleName)
  • To add a placeholder to the component use:
{this.props.placeholder.KEY}

Replace KEY with the placeholder key you want to create.

  • To make the placeholder dynamic, prepend the key with $Id.
{this.props.placeholder.$Id.KEY}

About

A ReactJS module for building Sitecore components with React

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 97.9%
  • PowerShell 2.1%