Created
May 1, 2015 20:08
-
-
Save emdagon/944472f39b58875045b6 to your computer and use it in GitHub Desktop.
Simple Component to include Meteor Templates on React Components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see https://github.com/reactjs/react-meteor | |
# and https://github.com/jhartma/meteor-cjsx | |
@IncludeTemplate = React.createClass | |
componentDidMount: () -> | |
componentRoot = React.findDOMNode(@) | |
parentNode = componentRoot.parentNode | |
parentNode.removeChild(componentRoot); | |
Blaze.render @props.template, parentNode | |
render: (template) -> | |
<div /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see https://github.com/reactjs/react-meteor | |
var IncludeTemplate = React.createClass({ | |
componentDidMount: function() { | |
var componentRoot = React.findDOMNode(this); | |
var parentNode = componentRoot.parentNode; | |
parentNode.removeChild(componentRoot); | |
return Blaze.render(this.props.template, parentNode); | |
}, | |
render: function(template) { | |
return (<div />) | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see https://github.com/reactjs/react-meteor | |
# and https://github.com/jhartma/meteor-cjsx | |
ReactMeteor.createClass | |
templateName: 'NavbarRight' | |
getMeteorState: () -> {} | |
render: () -> | |
<div id="navbar" className="navbar-collapse collapse"> | |
<ul className="nav navbar-nav navbar-right"> | |
<li><a href="#">Dashboard</a></li> | |
<IncludeTemplate template={Template._loginButtons} /> | |
</ul> | |
</div> |
It must be renderWithData I guess. The snippet working for me is:
IncludeBlaze = React.createClass({
propTypes: {
template: React.PropTypes.object.isRequired
},
componentDidMount() {
var componentRoot = ReactDOM.findDOMNode(this);
var parentNode = componentRoot.parentNode;
// Render the Blaze template on this node
this.view = Blaze.renderWithData(this.props.template, this.props.data, parentNode, componentRoot);
parentNode.removeChild(componentRoot);
},
componentWillUnmount() {
// Clean up Blaze view
Blaze.remove(this.view);
},
render() {
return <div />;
}
});
The snippet shown in previous comments is not working as expected. The reason is, it tries to render as last child of parent element after all other elements already rendered, not in the place where you leave <IncludeTemplate .../>
tag.
Thanks! Works really nice.
For SSR i propose using a string to identify the template, becaues Template is not defined on the server, i.e.
IncludeBlaze = React.createClass({
propTypes: {
template: React.PropTypes.string.isRequired
},
componentDidMount() {
var componentRoot = ReactDOM.findDOMNode(this);
var parentNode = componentRoot.parentNode;
// Render the Blaze template on this node
this.view = Blaze.renderWithData(Template[this.props.template], this.props.data, parentNode, componentRoot);
parentNode.removeChild(componentRoot);
},
componentWillUnmount() {
// Clean up Blaze view
Blaze.remove(this.view);
},
render() {
return <div />;
}
});
Thanks works super well. This is the variant I am using with refs:
IncludeBlaze = React.createClass({
propTypes: {
template: React.PropTypes.object.isRequired
},
componentDidMount() {
var componentRoot = ReactDOM.findDOMNode(this.container);
var parentNode = componentRoot.parentNode;
// Render the Blaze template on this node
this.view = Blaze.renderWithData(this.props.template, this.props.data, parentNode, componentRoot);
parentNode.removeChild(componentRoot);
},
componentWillUnmount() {
// Clean up Blaze view
Blaze.remove(this.view);
},
render() {
return (
<span
ref={(c) => this.container = c}
/>
);
}
});
module.exports = IncludeBlaze;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Batistleman : return Blaze.render(this.props.template, this.props.data, parentNode);
Does this statement work because
Blaze.render = function (content, parentElement, nextNode, parentView) , where does it accept data?