Package Exports
- react-habitat
- react-habitat/lib/Habitat
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-habitat) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
React Habitat 
React Habitat <3 Your CMS
React Habitat is designed for integrating React with your CMS using the DOM as the interface. It's based of some basic container programming principles and brings peace and order to multi page apps.
This framework exists so you can get on with the fun stuff!
Table of contents
- When to use React Habitat
- When not to use it
- Features
- Compatibility
- Installing
- Getting Started
- Options and Methods
- Contribute
- License information
When to use React Habitat
You should use React Habitat any time there is a framework or CMS rendering your HTML and you want one or multiple React components on the page(s). For example sometimes there are only sections of your page that you want to be a React Component, then this framework is perfect for that.
The idea behind this is that, rather than trying to initiate one or many React components; by either hard coding or using a Router. You switch it around so components "new up" themselves when required.
React Habitat works great with:
- Sitecore
- Adobe Experience Manager
- Umbraco
- Drupal
- Joomla
- Wordpress
- Magento
- ...etc
When not to use it
Typically if you're building a full-on one page React app that yanks data from restful API's... then this framework isn't really going to bring much benefit to you. However you are definitely invited to use it if you want to.
Features
- Tiny code footprint (only 8KB)
- Redux supported by including react-habitat-redux
- Pass data (props) to your components directly from HTML attributes and back again
- Automatic data/JSON parsing
- All page child apps can still share the same components, stores, events etc. (Everything is connected)
- Simple to swap out components for others (The beauty of IOC containers)
- For advanced users, you can use different components for different build environments
- 100% W3C HTML5 Valid
- TypeScript definitions included
Compatibility
- Supports Browsers IE9+ and all the evergreens.
- ES5, ES6/7 & TypeScript
- React v15 and up
We highly recommend you use something like WebPack or Browserify when using this framework.
Installing
Install with Node Package Manager (NPM)
npm install --save-dev react-habitat
This assumes that you’re using npm package manager with a module bundler like Webpack or Browserify.
If you don’t yet use npm or a modern module bundler, and would rather prefer a single-file UMD build that makes ReactHabitat
available as a global object, you can grab a pre-built version from the dist folder.
Getting Started
Using ES5? Read the ES5 version here.
The basic pattern for integrating React Habitat into your application is:
- Structure your app with inversion of control (IoC) in mind.
- At application startup...
- Create a Container.
- Register React components.
- Set the container for later use in the DOM.
- At application execution...
- Use the DOM scope to resolve instances of the components.
This getting started guide walks you through these steps for a simple React application. This document assumes you already know:
- How to compile JSX; and
- How to bundle using something like webpack or browserify
1. Create a bootstrapper class
The class must extend ReactHabitat.Bootstrapper
and is intended to be a entry point of your bundled app. So if you're using something like webpack or browserify then this is file to point it too.
In the constructor() of the class you need to register your React components with it and then set the container. The container is later bound to the DOM automatically so your React components self-initiate.
In React Habitat, you'd register a component something like this
container.register('SomeReactComponent', SomeReactComponent);
So for our sample application we need to register all of our components (classes) to be exposed to the DOM so things get wired up nicely.
import ReactHabitat from 'react-habitat';
import SomeReactComponent from './SomeReactComponent';
import AnotherReactComponent from './AnotherReactComponent';
class MyApp extends ReactHabitat.Bootstrapper {
constructor(){
super();
// Create a new container builder
var container = new ReactHabitat.Container();
// Register your top level component(s) (ie mini/child apps)
container.register('SomeReactComponent', SomeReactComponent);
container.register('AnotherReactComponent', AnotherReactComponent);
// Finally, set the container
this.setContainer(container);
}
}
// Always export a 'new' instance so it immediately evokes
export default new MyApp();
If you are using Redux
You will need to use a different container. Please install & configure the react-habitat-redux library. Then continue with step 2 below.
2. Application execution - render your components
During the web application execution you will want to make use of the components you registered. You do this by resolving them in the DOM from a scope.
When you resolve a component, a new instance of the object gets created (Resolving a component is roughly equivalent to calling 'new').
To resolve new instances of your components you need to attach a data-component
attribute to a div
or a span
element in the HTML. These elements should always
remain empty. Any child components should be nested inside the React components themselves.
Set the data-component
value to equal a component name you have registered in the container.
For instance:
<div data-component="SomeReactComponent"></div>
Will be resolved by the following registration.
container.register('SomeReactComponent', SomeReactComponent);
So, for our sample app we would do something like this
<html>
<body>
<div data-component="SomeReactComponent"></div>
<script src="myBundle.js" />
</body>
</html>
When you view this page you will see a instance of SomeReactComponent
automatically rendered in the div's
place. In fact, you can add as many as you like and it will render multiple instances.
For example. This is perfectly valid.
<html>
<body>
<div data-component="SomeReactComponent"></div>
<div data-component="SomeReactComponent"></div>
<div data-component="SomeReactComponent"></div>
<script src="myBundle.js" />
</body>
</html>
Will render 3 instances of your component.
Note It's important that the output built javascript file is included at the end of the DOM just before the closing