I want to redirect to Dashboard.jsx if the username and password matches. How to do that ? I am new to ReactJS.
in the If condition I want to add the redirect code to redirect another page. Pls response. In stackoverflow maximum are using without if condition so here is the difference.
var users={
name:'bddebashis',
password:'debashis111249'
}
class Home extends Component {
constructor() {
super();
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
const data = new FormData(event.target);
fetch('/api/form-submit-url', {
method: 'POST',
body: data,
});
if(data.get('usr')==users.name && data.get('paswd')==users.password){
<Redirect to='./Dashboard';/>
}
}
I want to redirect to Dashboard.jsx if the username and password matches. How to do that ? I am new to ReactJS.
in the If condition I want to add the redirect code to redirect another page. Pls response. In stackoverflow maximum are using without if condition so here is the difference.
var users={
name:'bddebashis',
password:'debashis111249'
}
class Home extends Component {
constructor() {
super();
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
const data = new FormData(event.target);
fetch('/api/form-submit-url', {
method: 'POST',
body: data,
});
if(data.get('usr')==users.name && data.get('paswd')==users.password){
<Redirect to='./Dashboard';/>
}
}
Share
Improve this question
edited May 14, 2018 at 12:03
Bashis
asked May 14, 2018 at 11:38
BashisBashis
231 gold badge1 silver badge5 bronze badges
2
-
remove the dot from redirect , it should be
to='/Dashboard'
, your question has already an answear here :stackoverflow./questions/43230194/… – Abslen Char Commented May 14, 2018 at 11:43 - Possible duplicate of How to use Redirect in the new react-router-dom of Reactjs – Abslen Char Commented May 14, 2018 at 11:45
4 Answers
Reset to default 5// Are You using BrowserRouter means you can use like this
import PropTypes from 'prop-types';
var users={
name:'bddebashis',
password:'debashis111249'
}
class Home extends Component {
constructor() {
super();
this.handleSubmit = this.handleSubmit.bind(this);
}
static contextTypes = {
router: PropTypes.object,
}
handleSubmit(event) {
event.preventDefault();
const data = new FormData(event.target);
fetch('/api/form-submit-url', {
method: 'POST',
body: data,
});
if(data.get('usr')==users.name && data.get('paswd')==users.password){
this.context.router.history.push("/Dashboard")
}
}
}
If you're using React Router you can use Redirect ponent
https://github./ReactTraining/react-router/blob/master/packages/react-router/docs/api/Redirect.md
<Redirect to="/dashboard" />
Adding Redirect ponent inside your render function should be sufficient.
import createHistory from 'history/createBrowserHistory';
export const history = createHistory();
<Router history={history}>
<Route />
<Router>
In you Dashboard ponent import history in dashboard use this line to redirect
history.push('/Dashboard');
Redirect makes easier by using history
module. Install history module npm install history
then configure your add router like this.
AppRouter.js
import { Router, Route, Switch } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
export const history = createHistory();
<Router history={history}>
<Route path="/about" ponent={AboutPage} />
<Route ... />
...
<Router>
then then redirect to another ponent.like
import {history} from './AppRouter';
history.push('/dashboard');