reactjs - 自动反应按下按钮
问题描述
在设置身份验证时,我遇到了一个问题,即只有通过按钮触发身份验证才能触发身份验证。
所以,我很想知道这是否是一种在 React 中自动触发按钮的 Onclick 事件的方法。
render() {
const { isAuthenticated } = this.props.auth;
return (
<div>
{
!isAuthenticated() && (
<Button
bsStyle="primary"
className="btn-margin"
onClick={this.login.bind(this)}
>
Login
</Button>
)
}
{
isAuthenticated() && (
<Button
bsStyle="primary"
className="btn-margin"
onClick={this.logout.bind(this)}
>
Logout
</Button>
)
}
</div>
);
}
上面的代码是我的渲染方法,我想自动触发登录按钮!
提前谢谢各位!
解决方案
你不能这样做,JSX
你需要使用componentWillReceieProps()
componentWillReceieProps(nextProps){
if(nextProps.auth !== this.props.auth && this.props.auth){
this.logout();
}else if(nextProps.auth !== this.props.auth && !this.props.auth){
this.login();
}
}
推荐阅读
- javascript - 直接定义方法和属性或者在js中使用getter
- react-native - 嵌套导航器在 React Navigation 中的高效处理
- python - TypeError: perform_create() does not have 1 required positional argument'serializer' 我收到这样的错误
- gradle - 在地图循环中运行 gradle 任务时,该任务仅在脚本之后运行
- mysql - 对于内部有很多数据库和很多表的 mysql 实例,最好的 mysql 配置是什么?
- windows - 通过 docker-compose 将 build-args 传递给 Dockerfile 的奇怪行为
- python - 时间序列数据合并最近右数据集具有多个相同的值
- c++ - 编写输出文件 Cuda C++
- ruby-on-rails - Rails 应用程序支持不同的时区
- node.js - Firebase 部署在控制台中有效,但在 bitbucket 管道中无效