I have 2 routes, /
and /about
and i've tested with several more. All routes only render the home ponent which is /
.
When I try a route that doesn't exist it recognises that fine and displays the warning
Warning: No route matches path "/example". Make sure you have <Route path="/example"> somewhere in your routes
App.js
import React from 'react';
import Router from 'react-router';
import { DefaultRoute, Link, Route, RouteHandler } from 'react-router';
import {Home, About} from './ponents/Main';
let routes = (
<Route name="home" path="/" handler={Home} >
<Route name="about" handler={About} />
</Route>
);
Router.run(routes, function (Handler) {
React.render(<Handler/>, document.body);
});
./ponents/Main
import React from 'react';
var Home = React.createClass({
render() {
return <div> this is the main ponent </div>
}
});
var About = React.createClass({
render(){
return <div>This is the about</div>
}
});
export default {
Home,About
};
I've tried adding an explicit path to about to no avail.
<Route name="about" path="/about" handler={About} />
I've stumbled upon this stackoverflow Q but found no salvation in its answer.
Can anyone shed some light on what might be the problem?
I have 2 routes, /
and /about
and i've tested with several more. All routes only render the home ponent which is /
.
When I try a route that doesn't exist it recognises that fine and displays the warning
Warning: No route matches path "/example". Make sure you have <Route path="/example"> somewhere in your routes
App.js
import React from 'react';
import Router from 'react-router';
import { DefaultRoute, Link, Route, RouteHandler } from 'react-router';
import {Home, About} from './ponents/Main';
let routes = (
<Route name="home" path="/" handler={Home} >
<Route name="about" handler={About} />
</Route>
);
Router.run(routes, function (Handler) {
React.render(<Handler/>, document.body);
});
./ponents/Main
import React from 'react';
var Home = React.createClass({
render() {
return <div> this is the main ponent </div>
}
});
var About = React.createClass({
render(){
return <div>This is the about</div>
}
});
export default {
Home,About
};
I've tried adding an explicit path to about to no avail.
<Route name="about" path="/about" handler={About} />
I've stumbled upon this stackoverflow Q but found no salvation in its answer.
Can anyone shed some light on what might be the problem?
Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Sep 6, 2015 at 8:34 KieeKiee 10.8k8 gold badges35 silver badges56 bronze badges2 Answers
Reset to default 5Using ES6 and the newest react-router would look like this:
import React from 'react';
import {
Router,
Route,
IndexRoute,
}
from 'react-router';
const routes = (
<Router>
<Route ponent={Home} path="/">
<IndexRoute ponent={About}/>
</Route>
</Router>
);
const Home = React.createClass({
render() {
return (
<div> this is the main ponent
{this.props.children}
</div>
);
}
});
//Remember to have your about ponent either imported or
//defined somewhere
React.render(routes, document.body);
On a side note, if you want to match unfound route to a specific view, use this:
<Route ponent={NotFound} path="*"></Route>
Notice the path is set to *
Also write your own NotFound ponent.
Mine looks like this:
const NotFound = React.createClass({
render(){
let _location = window.location.href;
return(
<div className="notfound-card">
<div className="content">
<a className="header">404 Invalid URL</a >
</div>
<hr></hr>
<div className="description">
<p>
You have reached:
</p>
<p className="location">
{_location}
</p>
</div>
</div>
);
}
});
Since you've nested About
under Home
you need to render a <RouteHandler />
ponent within your Home
ponent in order for React Router to be able to display your route ponents.
import {RouteHandler} from 'react-router';
var Home = React.createClass({
render() {
return (<div> this is the main ponent
<RouteHandler />
</div>);
}
});