3 回答

TA貢獻1936條經驗 獲得超7個贊
如果你正在使用,那么你應該像這樣使用react-router
console.log(values);
const formData = new FormData();
formData.append("username", values.username);
formData.append("password", values.password);
const options = {
method: 'POST',
body: formData
};
fetch('http://localhost:8000/api/login', options).then((response) => {
notification.success({
message: "login successfully."
});
this.props.history.push('/your-homepage-route') <---- Navigate after successful login
}).catch(error => {
alert('Login Failed. Try Again') <----- Error Handling
});
};```

TA貢獻1828條經驗 獲得超3個贊
我認為對于重定向,如果您使用包進行路由,這將很容易:react-router-dom
如果您將其用于路由,例如,這會對此組件進行增壓,從而可以輕松進行編程重定向:
<Route path="/login" component={Login}/>
this.props.history.push('/home')
否則,您需要使用同一包中的高度順序組件,然后包裝您的組件,最后用于重定向
import { withRouter} from 'react-router-dom'
withRouter(Login)
this.props.history.push('/home')

TA貢獻1842條經驗 獲得超13個贊
要重定向到新頁面,這不依賴于 react:
window.location = 'http://www.whateveryourpathis.com';
添加回答
舉報