I'm currently trying to write an app with Cordova and ReactJS. I haven't used ReactJS before, so I'm very confused by the mixed usage of Babel, JavaScript and JSX. I try to use JSX where I can because I like it, but I dislike Babel, which is used in many tutorials as well. I haven't quite understood what Babel is used for at all.
const { Router,
Route,
IndexRoute,
Redirect,
Link,
IndexLink
} = ReactRouter
var Header = React.createClass({
render: function () {
return (
<h1 className="title">{this.props.text}</h1>
);
}
});
var Content = React.createClass({
render: function() {
return (
<h1 className="title">{this.props.text}</h1>
);
}
});
var Page = React.createClass({
getInitialState: function(){
return {
'title': 'Main Page' + (this.props.title ? ' - ' + this.props.title : '')
}
},
render: function() {
return (
<div>
<Header text={this.state.title} />
<Content text={this.props.text} />
</div>
);
}
});
var Home = React.createClass({
getInitialState: function(){
return {
'title': 'Home',
'text': <Link to="/about">About</Link>
}
},
render: function(){
return (
<Page title={this.state.title} text={this.state.text} />
);
}
});
var About = React.createClass({
getInitialState: function(){
return {
'title': 'About',
'text': 'Lalal'
}
},
render: function(){
return (
<Page title={this.state.title} text={this.state.text} />
);
}
});
React.render((
<Router>
<Route path="/" component={Home} />
<Route path="/about" component={About} />
</Router>
),
document.getElementById('app')
);
I'm wondering if there's a better way to do this or if I'm doing everything correctly. I did my best with the classes (thinking in React stuff) and wanted to make Header
a Navigation later on. Is there a good way to separate the routes, and put my views into files and components where needed?