10

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.

5
  • 1
    It'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. Commented 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. Commented Mar 2, 2016 at 10:29
  • Have you followed the official tutorial? github.com/reactjs/react-router-tutorial Commented 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'; Commented 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). Commented Mar 2, 2016 at 10:56

3 Answers 3

10

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'))
Sign up to request clarification or add additional context in comments.

Comments

2

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

what I had, an importing issue of react router without using ES6. "ReactRouter.Router" solved it.
This is exactly what I needed to do. Saved me hours of work.
0

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
};

5 Comments

" import React from 'react'; " not working in browser. I tried using requirejs also. but still problem is not solved.
This is just an example. It will work only if you use webpack and bible
I am finding a way without using node.js
React has nothing to do with node.js. you can use it with any server you like
ok. it is very helpful if you can exaplain me how to integrate react router to the app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.