Reputation: 357
There is a login component. My application entry point is login page. After successful login, moved to main page(dashboard & navigation). After successful login main page is displaying with nav component and dashboard.But navigations are not working. I am getting error:
react-router Location "/examples" did not match any routes react-router Location "/dashboard" did not match any routes
My login Component is
var React = require('react');
var {Link} = require('react-router');
var Dashboard = require('Dashboard');
var Login = React.createClass ({
onFormSubmit: function(e){
e.preventDefault();
window.location.href = '/main'
},
render: function(){
return (
<div>
<h1 className="text-center">Login</h1>
<form onSubmit={this.onFormSubmit}>
<input type="text" ref="username"/>
<button className="button">Login</button>
</form>
</div>
)
}
});
module.exports = Login;
My Dashboard component is
var React = require('react');
var Nav = require('Nav');
var Dashboard = (props) => {
return (
<div>
<h1> Dashboard</h1>
</div>
</div>
);
}
module.exports = Dashboard;
Main component Main.jsx
var React = require('react');
var Nav = require('Nav');
var Main = (props) => {
return (
<div>
<h1> Main</h1>
<Nav/>
<div className="row">
<div className="columns medium-6 large-4 small-centered">
{props.children}
</div>
</div>
</div>
);
}
module.exports = Main;
Main app.jsx file is
var React = require('react');
var ReactDOM = require('react-dom');
var {Route, Router, IndexRoute, browserHistory} = require('react- router');
var Login = require('Login');
var Dashboard = require('Dashboard');
var About = require('About');
var Examples = require('Examples');
var Main = require('Main');
require('style!css!foundation-sites/dist/foundation.min.css')
$(document).foundation();
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={Login}/>
<Route path="/main" component={Main}>
<Route path="about" component={About}/>
<Route path="examples" component={Examples}/>
<IndexRoute component={Dashboard}/>
</Route>
</Router>,
document.getElementById('app')
);
My server.js file:
var express = require('express');
// Create our app
var app = express();
app.get(['/', '/main'], function(req, res){
res.sendfile('./public/index.html')
});
app.use(express.static('public'));
app.listen(3000, function () {
console.log('Express server is up on port 3000');
});
Upvotes: 3
Views: 7699
Reputation: 3516
React Router v4 with webpack Implementation as below
import { BrowserRouter as Router, Route } from 'react-router-dom';
ReactDOM.render(
<Router>
<div>
<Route exact path="/" render={() => <h1>main page</h1>} />
<Route path="/about" render={() => <h1>about page</h1>} />
<Route path="/contact" render={() => <h1>contact page</h1>} />
</div>
</Router>,
document.getElementById('root'));
//webpack build script change add --history-api-fallback in package.json file
"scripts": {
"start": "webpack-dev-server --history-api-fallback"
}
Now run following command on terminal: npm start
Upvotes: 0
Reputation: 5656
As far as I can tell you are routing to /main
on login which is why your Dashboard
and Main
page is working. If you want to view the examples
page you need to redirect to /main/examples
and if about
then /main/about
. Also you should always redirect using the react-router
API. Check out this answer to figure out how to redirect programmatically using react-router
Upvotes: 0
Reputation: 137
You declared your application routes like so:
<Route path="/main" component={Main}>
<Route path="about" component={About}/>
<Route path="examples" component={Examples}/>
<IndexRoute component={Dashboard}/>
</Route>
So you should be using:
/main/examples
/main/dashboard
instead of:
/examples
/dashboard
Upvotes: 1