reactjs - How can I have parent component to route to home page once child component has a success in Signup?
问题描述
I want my parent component App.js
to route to '/home'
if the child component LoginApp
has a response back. (All validation happening is server side hence response will only comeback if authentication succeeds).
// Child Component
const LoginApp = () => {
const signupWasClickedCallback = (data) => {
console.log(data);
// ----- Adding Favourites
axios.post('/api/account/signup', data)
.then(function (res) {
console.log(res);
})
.catch(function (err) {
console.log(err);
});
};
const loginWasClickedCallback = (data) => {
console.log(data);
axios.post('/api/account/signin', data)
.then(function (res) {
console.log(res);
})
.catch(function (err) {
console.log(err);
});
};
return (
<div className="loginWrapper">
<ReactSignupLoginComponent
title="Welcome to KidKlub!"
handleSignup={signupWasClickedCallback}
handleLogin={loginWasClickedCallback}
/>
</div>
);
};
export default LoginApp;
// Parent Component
class App extends Component {
render() {
return (
<Router>
<div>
<Route exact path="/" component={LoginApp} />
<Route exact path="/home" component={Home} />
</div>
</Router>
);
}
}
export default App;
解决方案
你可以这样做:
.then(function (res) {
console.log(res);
props.history.push('/home');
})
.catch(function (err) {
console.log(err);
props.history.push('/');
});
从 react-router-dom 使用 withRouter
import { withRouter } from 'react-router-dom';
用 withRouter 包装你的组件
export default withRouter(LoginApp);
推荐阅读
- android - Android Studio 模拟器:警告 System.cpp
- python - 使用 html、python 和 mysql 创建一个带有下拉菜单的网页
- python - Selenium 在光标活动部分键入文本
- elasticsearch - ElasticSearch 7.5 中 TermFilter() 的替代方法是什么
- windows - ShellExecute,路径中的空格
- apache-flink - 在同一源上 Flink 多个 Windows
- laravel - Laravel Socialite 回调 auth('customer')->user() 为 null
- python - 如何将 3 个列表映射到字典键
- python - 在 TF(2.0) 模型中使用循环缓冲区
- php - 将编码从 windows-1251 更改为 utf-8