reactjs - ReactJS:条件为真后如何进入下一页?
问题描述
满足条件后如何自动跳转到下一页?
handleSubmit = event => {
event.preventDefault();
const { data } = this.state
const tabletopModel = data['token']
const newData = tabletopModel ? tabletopModel.all() : null
console.log('log for newdata', newData)
var c = 0;
if (newData == null) {
console.log('null')
}
else {
for (var i = 0; i < newData.length; i++) {
if (newData[i].ID == this.input.value) {
c++;
//console.log('i', i);
}
}
if (c > 0)
alert('Your username is: ' + this.input.value);
//GO TO NEXT PAGE
else {
alert('Invalid login');
}
}
};
这是一个按钮存在的代码,点击它会运行这部分
这是正确的还是有更好的解决方案?
解决方案
this.props.history.push
如果您在一个类组件中或者它是一个功能组件,您可以使用props.history.push
,您还可以将状态传递给您要重定向到的页面,例如:
this.props.history.push({
pathname: '/next-page',
state: { from: "prev-page" }
})
通过的状态将存储在props.location.state
下一页中,
你也可以替换这个:
for (var i = 0; i < newData.length; i++) {
if (newData[i].ID == this.input.value) {
c++;
//console.log('i', i);
}
}
有了这个:
if(newData.some(currentValue => currentValue.ID === this.input.value)) {
c++;
//console.log('i', i);
}
推荐阅读
- java - mysql-binlog-connector-java - 在二进制日志索引文件中找不到第一个日志文件名
- java - IBM AppScan 的验证要求问题
- openssl - 根 CA 证书和中间 CA 证书
- docker - 使用 docker 和 docker-compose 构建的工作流程是什么?
- javascript - 在 webpack + react 中使用 sass-loader 出现多个错误
- java - Reactor 调度程序实施注意事项
- javascript - 将 HTML 对象添加到事件 javascript/meteor
- c# - 找不到方法 Microsoft.Owin.Security.Notifications.MessageReceivedNotification
- c++ - C++调用另一个子类的公共方法
- python - TesseractError: (-1073741819, u'') 使用 pytesseract 时