I am using react.js but not using nodes server. I need a solution for routing, but it seems react-router is working with node. if that is not, give me some example how to integrate react router. if not I need another router which working with react.
-
1It's not really clear what you are asking. React router works at the client side too, because it's implemented with React components. There's no requirement to use Node.Dan Prince– Dan Prince2016-03-02 10:17:06 +00:00Commented Mar 2, 2016 at 10:17
-
can you please give me clear example? I am not clear how to import and initialize react router in my app.semira– semira2016-03-02 10:29:48 +00:00Commented Mar 2, 2016 at 10:29
-
Have you followed the official tutorial? github.com/reactjs/react-router-tutorialDan Prince– Dan Prince2016-03-02 10:32:31 +00:00Commented Mar 2, 2016 at 10:32
-
yes i did it with several resources. but what I didn't understand below code : import React from 'react'; import Router from 'react-router'; import { DefaultRoute, Link, Route, RouteHandler } from 'react-router';semira– semira2016-03-02 10:38:00 +00:00Commented Mar 2, 2016 at 10:38
-
It's ES6 syntax, the tutorial expects you to be using an ES6 environment or transpiling your code with [babel](babeljs.io).Dan Prince– Dan Prince2016-03-02 10:56:33 +00:00Commented Mar 2, 2016 at 10:56
3 Answers
Building off of @semira here is how I implemented a buildless react application.
Inside of ./index.html (or ./index.php in my case) we load the React library, React router, and the Babel transpiler that will convert ES6 javascript so that browsers can interpret it. Note that you have to explicitly state the script type as text/babel in order for the Babel transpiler to convert ES6 javascript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>React App Boilerplate</title>
<!-- Foundation 6.3.0 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.0/css/foundation.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.0/js/foundation.min.js"></script>
</head>
<body>
<div id="app"></div>
<!------------------------------------------------------------------------------------------------------------------>
<!-- Dependencies -->
<!------------------------------------------------------------------------------------------------------------------>
<!-- Babel ES6 to ES5 Transpiler -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<!-- React Library -->
<script src="https://unpkg.com/[email protected]/dist/react.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-dom.min.js"></script>
<!-- React Router -->
<script src="https://unpkg.com/react-router/umd/ReactRouter.min.js"></script>
<!------------------------------------------------------------------------------------------------------------------>
<!-- React Components -->
<!------------------------------------------------------------------------------------------------------------------>
<script type="text/babel" src="app/components/App.js"></script>
<script type="text/babel" src="app/components/About.js"></script>
<!-- Application entry point -->
<script type="text/babel" src="app/app.js"></script>
</body>
</html>
Where ./app/components/About.js is a stateless React component written in ES6:
const About = () => <h2>About Page</h2>;
Where ./app/components/App.js is a React component written in ES6:
class App extends React.Component {
render() {
return (
<div>
<h2>App Page</h2>
</div>
);
}
}
Then inside of .app/app.js notice how we can still make the code look like the documentation by storing the ReactDOM and ReactRouter as references.
let render = ReactDOM.render;
let browserHistory = ReactRouter.browserHistory;
let Router = ReactRouter.Router;
let Route = ReactRouter.Route;
let IndexRoute = ReactRouter.IndexRoute;
let Link = ReactRouter.Link;
render((
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="about" component={About} />
</Route>
</Router>
), document.getElementById('app'))
Comments
Finally I found what I was finding. Thanks for all the help.
var routes = (
<ReactRouter.Router>
<ReactRouter.Route name="app" path="/" component={App}/>
<ReactRouter.Route name="app" path="new" component={CreateApp}/>
</ReactRouter.Router>
);
ReactDOM.render(
<ReactRouter.Router>{routes}</ReactRouter.Router>,
document.getElementById('content')
);
2 Comments
React can work with any server that return HTML file. The html file includes a JS file which looks like this:
import React from 'react';
import ReactDOM from 'react-dom';
import Root from './js/containers/root';
import createBrowserHistory from 'history/lib/createBrowserHistory';
const history = createBrowserHistory();
ReactDOM.render(
<Root history={ history } />,
document.getElementById('app')
);
And the routing can be manage by react-router which looks something like this:
import React, {PropTypes, Component} from 'react';
import { IndexRoute, Router, Route } from 'react-router';
import { Provider } from 'react-redux';
import COMPONENTa from './ComponentA';
import COMPONENTb from './ComponentB';
import COMPONENTc './ComponentC';
class Root extends Component {
render() {
const { history } = this.props;
return (
<Router history={ history }>
<Route path="/PATH/PATH/" component={ COMPONENTa }>
<IndexRoute component={ COMPONENTb } />
<Route path="PARAMS/:id" component={ COMPONENTc } />
</Route>
</Router>
);
}
}
Root.propTypes = {
history: PropTypes.object.isRequired
};