React Router 1.0 Multiple <route/> - reactjs

How can add multiple root routes? My login page's html structure is completely different than other Authenticated Pages(DashBoard, managesubscription, etc.)
I created login.jsx somthing like as follows:-
var React = require('react');
var Login = React.createClass({
displayName: 'Login',
render() {
return (
<div>
<div>Login</div>
<form>
User Name: <input type="text" name="username"/>
Password<input type="password" name="password"/>
<input type="submit"/>
</form>
</div>
);
}
});
module.exports = Login;
Router.js
ReactDOM.render((
<Router history={History}>
<Route path="/" component={App}>
<Route path="login" component={Login}/>
<Route path="dashboard" component={Dashboard} />
<IndexRoute component={Login}/>
</Route>
</Router>
), document.getElementById('tstdiv'));
app.jsx
var React = require('react');
var Router = require('react-router');
var App = React.createClass({
render: function(){
return (
<div>
{this.props.children}
</div>
);
}
});
module.exports = App;
How can i create AuthenticatedApp.jsx(for dashboard,managesubscription) like as following?
var React = require('react');
var Router = require('react-router');
var AuthenticatedApp = React.createClass({
render: function(){
return (
<div>
<Header/>
{this.props.children}
<Footer/>
</div>
);
}
});
module.exports = AuthenticatedApp;

One option could be to change your App.jsx into something like this:
var React = require('react');
var Router = require('react-router');
var Header = require('path/to/header');
var Footer = require('path/to/footer');
var App = React.createClass({
render: function(){
if (this.props.location.pathname === '/login') {
Header = null;
Footer = null;
}
return (
<div>
<Header />
{this.props.children}
<Footer />
</div>
);
}
});
Basically if it's the login page, we hide the header and footer, in every other case (the authenticated pages) will display the header and footer.
However I have a feeling that there is another way of doing this by changing how the routes are set up, so we can remove the if condition in this render.

Related

How does React-Router work?

I am using express and trying to implement partial pieces of react as sort of a demo. But one react component renders separately on a different page while the rest render on one. I assume react router is perfect this since I can route the page to the component so only one component will render if on that page.
Index.jsx
import { Router, Route, Redirect } from 'react-router-dom';
var Projects = React.createClass({
render: function() {
return (
<div className ="project-page">
<Change />
<Mortgage />
<Todo />
<Astro />
</div>
);
}
});
var Landing = React.createClass({
render: function() {
return (
<div className ="landing-page">
<App />
</div>
);
}
});
var MyApp = React.createClass ({
render: function(){
return(
<Router history={history}>
<Route path="/projects" component={Projects} />
<Route path="/" component={Landing} />
</Router>
);
}
});
ReactDOM.render(<MyApp />, document.getElementById('output'))
export default index;

Navigation from component to another componet

I have login component and dashboard. My application entry point is login page. After successful login, I want move to main page(dashboard & navigation).
I tried this like the following but its not working. After login not able to move to dashboard.
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();
<Dashboard />
},
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>
<Nav/>
<div className="row">
<div className="columns medium-6 large-4 small-centered">
{props.children}
</div>
</div>
</div>
);
}
module.exports = Dashboard;
Main app.jsx file is
var React = require('react');
var ReactDOM = require('react-dom');
var {Route, Router, IndexRoute, hashHistory} = require('react-router');
var Login = require('Login');
var Dashboard = require('Dashboard');
var About = require('About');
var Examples = require('Examples');
ReactDOM.render(
<Router history={hashHistory}>
<Route path="/" component={Login}>
<Route component={Dashboard}>
<Route path="about" component={About}/>
<Route path="examples" component={Examples}/>
</Route>
</Route>
</Router>,
document.getElementById('app')
);
Your onFormSumit() should be like this
onFormSubmit: function(e){
e.preventDefault();
window.location.href = '/dashboard'
},
And your <Router/> in app.jsx should be like this
<Router history={hashHistory}>
<Route path="/" component={Login} />
<Route path="dashboard" component={Dashboard} />
<Route path="about" component={About}/>
<Route path="examples" component={Examples}/>
</Router>
This will make the browser redirect to /dashboard on form submit. That redirected url(/dashboard) will be captured by the <Router/> and the component for that path will be rendered.
[Update]
In your express, you nee to add the path so that it always returns the index.html page. Add this
app.get(['/', '/dashboard'], function(req, res){
res.sendfile('./path to your index.html file')
})
or you can do the following also
app.get('*', function(req, res){
res.sendfile('./path to your index.html file')
})
For further read, check this

React issue with route

I have started to experiment with react following this tutorial: http://courses.reactjsprogram.com/
I have the following code for my route.s
var React = require('react');
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var IndexRoute = ReactRouter.IndexRoute;
var hashHistory = ReactRouter.hashHistory;
var Main = require('../components/Main');
var Home = require('../components/Home');
var PromptContainer = require('../containers/PromptContainer');
var routes = (
<Router history={hashHistory}>
<Route path='/' component={Main}>
<IndexRoute component={Home} />
<Route path='playerOne' header='Player One' component={PromptContainer} />
<Route path='playerTwo/:playerOne' header='Player Two' component={PromptContainer} />
</Route>
</Router>
);
module.exports = routes;
and PromptContainer.js as follow:
var React = require('react');
var PromptContainer = React.createClass({
render: function () {
return (
<div className="jumbotron col-sm-6 col-sm-offset-3 text-center">
<h1>{this.props.route.header}</h1>
<div className="col-sm-12">
<form>
<div className="form-group">
<input
className="form-control"
placeholder="Github Username"
type="text" />
</div>
<div className="form-group col-sm-4 col-sm-offset-4">
<button
className="btn btn-block btn-success"
type="submit">
Continue
</button>
</div>
</form>
</div>
</div>
)
}
});
module.exports = PromptContainer;
index.js:
var React = require('react');
var ReactDOM = require('react-dom');
var routes = require('./config/routes');
ReactDOM.render(
routes,
document.getElementById('app')
);
when viewed in the browser I get the following error:
index_bundle.js:21001 Warning: [react-router] Location "//playerTwo" did not match any routes
Any help?
You are importing "Route" incorrectly.
this
var Route = ReactRouter.Router;
should be
var Route = ReactRouter.Route;
You have a route defined for /playerTwo/USERNAME but no route defined for just /playerTwo/, thus the error. The code only knows how to handle that URL when it also has the PlayerOne username on the end.
Unfortunately your code doesn't pass the PlayerOne username into the route - to do that, you'll need a few extra functions to handle grabbing the input value and appending it to the URL.
Checkout Video 5 in the React.js Fundamentals course for the step-by-step answer on how to handle it all.

How to hide nav bar in login & signup page in react?

I have my routes like this:
<Route handler={AppContainer}>
<Route path="login" handler={Login} />
<Route path="signup" handler={Signup} />
<DefaultRoute handler={Home} />
</Route>
My AppContainer:
var AppContainer = React.createClass({
render: function () {
return (
<div>
<Header />
<RouteHandler />
</div>
);
}
});
Here the Header component gets rendered in all the pages. Is there a way to avoid header in login and sign up page.
This means that your component (AppContainer) has state. Therefore you should have something like:
var AppContainer = React.createClass({
// state management in here
render: function () {
let navHeader = this.state.isAuth ? <Header /> : '';
return (
<div>
{navHeader}
<RouteHandler />
</div>
);
}
});

React Router Renders Empty Script Tag

I am trying to figure out React Router, but when I load the page I am only seeing an empty script tag being rendered. I must be doing something wrong. I have followed the guide on the react router docs page.
var React = require('react');
var Router = require('react-router');
var Route = Router.Route;
var RouteHandler = Router.RouteHandler;
var About = require('./About');
var Inbox = require('./Inbox');
var routes = (
<Route handler={App}>
<Route path="about" handler={About}/>
<Route path="inbox" handler={Inbox}/>
</Route>
);
var App = React.createClass({
render () {
return (
<div>
<h1>App</h1>
<RouteHandler/>
</div>
)
}
});
Router.run(routes, Router.HashLocation, (Root) => {
React.render(<Root/>, document.getElementById('app'));
});
I figured it out. I needed to put my routes variable below App like so:
var React = require('react');
var Router = require('react-router');
var Route = Router.Route;
var RouteHandler = Router.RouteHandler;
var About = require('./About');
var Inbox = require('./Inbox');
var App = React.createClass({
render () {
return (
<div>
<h1>App</h1>
<RouteHandler/>
</div>
)
}
});
var routes = (
<Route handler={App}>
<Route path="about" handler={About}/>
<Route path="inbox" handler={Inbox}/>
</Route>
);
Router.run(routes, Router.HashLocation, (Root) => {
React.render(<Root/>, document.getElementById('app'));
});

Resources