React goes web component – Use custom tags to place react components directly in html.
- Install
- Basic Usage
- Tag Content
- Nested Components
- Expressions and Properties
- Case-Sensitive Attributes
- Html Comments
- API: mount( tags [, opts] );
- License
From Github, NPM:
$ npm install --save react-mount
or Bower:
$ bower install react-mount
With AMD or Browserify:
var mount = require("react-mount");
Vanilla JS:
<script src="path/to/file/react-mount.js"></script>
<script>
var mount = React.mount;
</script>
<body>
<react-component />
<!-- Dependencies -->
<script src="react.js"></script>
<script src="JSXTransformer.js"></script>
<script src="react-mount.js"></script>
<script>
// React component
var ReactComponent = React.createClass({
render: function() {
return React.createElement("i", null, "Component mounted. React is running.")
}
});
// Mount component
React.mount({
"react-component" : ReactComponent
});
</script>
</body>
Component mounted. React is running.
Content of custom tags can be written in Html or JSX.
<translucent-component>
<p style="text-transform: uppercase">
Html is transformed to <span style="font-weight:bold">JSX</span> for you.
</p>
</translucent-component>
The content of a custom tag is available in the mounted react component with this.props.children
:
var translucentComponent = React.createClass({
render: function() {
return (
{this.props.children}
);
}
});
HTML IS TRANSFORMED TO JSX FOR YOU.
Custom component tags can be nested as long as all used components are properly mounted:
<translucent-component>
<react-component />
</translucent-component>
mount({
"react-component" : ReactComponent,
"translucent-component" : TranslucentComponent
});
Component mounted. React is running.
{expressions}
can be used within a tag and are executed properly.
Within {expressions}
properties from the global namespace window
can be accessed.
However to keep the global namespace clear, properties can also be defined in the opts
object passed to the mount
function:
window.paragraph = "Component mounted. React is running.";
React.mount({
"translucent-component" : ReactComponent
},
{
props : {
list : [
"Item 1",
"Item 2",
"Item 3"
],
attribute : "myAttribute"
}
});
These properties are avaliable within expressions as props.key
:
{window.paragraph} // "Component mounted. React is running."
{props.list.length <= 3 ? "Yes!" : "No!"} // Yes!
// shortcut for simple reference (props. is appended for you)
{attribute} // "myAttribute"
<translucent-component attribute={attribute}>
<p>{window.paragraph}</p>
<p>Less than four list items? </b>{props.list.length <= 3 ? "Yes!" : "No!"}</b></p>
<ul>
{props.list.map(function(value, i){
return <li key={i} >{value}</li>;
})}
</ul>
</translucent-component>
Within react component:
this.props.attribute === "myAttribute"
this.props.children
contains <p>...</p>
, <p>...</p>
and <ul>...</ul>
.
Component mounted. React is running.
Less than four list items? Yes!
- Item 1
- Item 2
- Item 3
Html is case insensitiv and transforms everything outside of strings to lowercase. React props
however are case sensitive and therefore some components require correctly capitalized attributes.
There are two ways to preserve the capitalization of attributes:
A) Per component
mount({
"react-component" : ["camelCaseAttribute", "anotherAttribute", ReactComponent]
});
B) For all components
mount({
"react-component" : ReactComponent
}, {
preserveAttributes : ["camelCaseAttribute", "anotherAttribute"]
});
In both cases you can now safely use the preserved attributes:
<react-component camelCaseAttribute="preserved attribute" notPreservedAttribute="not preserved" />
Within react component:
this.props.camelCaseAttribute === "preserved attribute"
but
this.props.notPreservedAttribute === undefined
this.props.notpreservedattribute === "not preserved"
Html comments do not affect the output of the rendering in any way.
They can be used to mask unrendered content before react kicks in.
<translucent-component>
<!--
<i>{paragraph}</i>
-->
</translucent-component>
this.props.children
still contains <i>value</i>
.
value
Tipp: Use JSX style {/* comments */}
for actual comments.
Type
Object
Object with tags and their corresponding components to be mounted.
{ "tag-name" : ReactComponent, "tag-Name" : ReactComponent, ... "TAG-NAME" : ["camelCaseAttribute", ReactComponent], ... [tag] : [component | Array] }Tag names are case insensitive. All the above definitions would do the same / the first component would be mounted to all of the tags.
Optional: Instead of a React component, an array with case-sensitive attributes can be defined. The last item of the array is the react component to be mounted.
See: Case-Sensitive AttributesTipp: Lower case tag names containing at least one dash are/will be valid html.
See: http://w3c.github.io/webcomponents/spec/custom/#concepts
Type
Object
Available options for
react-mount
:{ "context" : [HTMLElement], "props" : {...}, "preserveAttributes" : [...], "wrapper" : [HTMLElement] }Type
HTMLElement
Defaultdocument.body
Only tags within this element will be mounted.
Type
Object
key:value
pairs of Properties.
Properties can be used in{expressions}
within the mounted tags.Type
Array
Array of case-sensitive attributes to preserve capitatization.
Type
HTMLElement
Defaultdocument.createElement("div")
Define a custom wrapper to mount react components into.
Copyright (c) 2015 Philipp Adrian, philippadrian.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.