React router redirect example

04-Apr-2023

.

Admin

Hello Friends,

In react js we have to use many time redirect to other url. sometimes onclick,after login,after call method and after submit we have to redirect to another component. if your redirect router not working then you have to check here. react router redirect after api call.react router redirect based on condition

In react you have to simple use "" tag. then provide you url in 'to' attribute.

class MyComponent extends React.Component {

state = {

redirect: false

}

handleSubmit () {

axios.post(/**/)

.then(() => this.setState({ redirect: true }));

}

render () {

const { redirect } = this.state;

if (redirect) {

return <Redirect to='/somewhere'/>;

}

return <RenderYourForm/>;

}

I hope it can help you...

#React.js