Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
63
rated 0 times [  68] [ 5]  / answers: 1 / hits: 39463  / 11 Years ago, sat, january 11, 2014, 12:00:00

Is there any way to move the jsx from a component's render function to a separate file? If so, how do I reference the jsx in the render function?


More From » reactjs

 Answers
50

Here is a pattern for separating the template jsx that uses CommonJS modules in NodeJS, Browserify or Webpack. In NodeJS, I found the node-jsx module helpful to avoid the need to compile the JSX.



// index.js
require('node-jsx').install({extension: '.jsx'});
var React = require('react'),
Component = require('./your-component');


// your-component.jsx
var YourComponent,
React = require('react'),
template = require('./templates/your-component.jsx');

module.exports = YourComponent = React.createClass({
render: function() {
return template.call(this);
}
});


// templates/your-component.jsx
/** @jsx React.DOM */
var React = require('react');

module.exports = function() {

return (
<div>
Your template content.
</div>
);

};


Update 2015-1-30: incorporated suggestion in Damon Smith's answer to set this in the template function to the React component.



Update 12/2016: the current best practice is to use the .js extension and use a build tool like Babel to output the final javascript from your source. Take a look at create-react-app if you're just getting started. Also, the latest React best practices do recommend a separation between components that manage state (typically called container components) and components that are presentational. These presentational components can now be written as functions, so they are not far off from the template function used in the previous example. Here is how I would recommend decoupling most of the presentational JSX code now. These examples still use the ES5 React.createClass() syntax.



// index.js
var React = require('react'),
ReactDOM = require('react-dom'),
YourComponent = require('./your-component');

ReactDOM.render(
React.createElement(YourComponent, {}, null),
document.getElementById('root')
);

// your-component.js
var React = require('react'),
YourComponentTemplate = require('./templates/your-component');

var YourComponentContainer = React.createClass({
getInitialState: function() {
return {
color: 'green'
};
},

toggleColor: function() {
this.setState({
color: this.state.color === 'green' ? 'blue' : 'green'
});
},

render: function() {
var componentProps = {
color: this.state.color,
onClick: this.toggleColor
};
return <YourComponentTemplate {...componentProps} />;
}
});

module.exports = YourComponentContainer;

// templates/your-component.js
var React = require('react');

module.exports = function YourComponentTemplate(props) {
return (
<div style={{color: props.color}} onClick={props.onClick}>
Your template content.
</div>
);
};

[#73241] Friday, January 10, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
billier

Total Points: 153
Total Questions: 85
Total Answers: 91

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
billier questions
Sun, Dec 27, 20, 00:00, 3 Years ago
Tue, May 26, 20, 00:00, 4 Years ago
Fri, Apr 3, 20, 00:00, 4 Years ago
;