【问题标题】:React button that authenticates with json and conditionally renders使用 json 进行身份验证并有条件地呈现的 React 按钮
【发布时间】:2017-12-15 21:23:38
【问题描述】:

我正在尝试在 React 中创建一个按钮,该按钮执行“如果 '/backend_authentication' 返回 true,则继续进入下一页,否则不要继续进入下一页”。我该怎么做?

注意:我只是想知道如何编写按钮来执行此操作。我不需要更改获取。

class Login extends Component {

handleClick(event) {
fetch('/backend_authentication')
.then(res => res.json())
}

render() {
return (
<div>       
         <LinkContainer to='/Homepage'>
             <Button onClick={this.handleClick} 
               type="submit"> /> 
              </Button>
         </LinkContainer>
</div>
);
}
}

【问题讨论】:

  • @uğuraydın 为什么? fetch 很好...... OP 不需要完全不同的包来完成他们正在寻找的东西。
  • 我只是想弄清楚如何编写这个 if 语句,而不是更改 fetch
  • 仅供参考,它具有适合您任务的出色功能。
  • 你好像在用 react-router,看这里:reacttraining.com/react-router/web/example/auth-workflow

标签: json function reactjs button conditional


【解决方案1】:

当您请求身份验证时,您可能会收到一些响应对象,因此,请检查响应数据是否成功或失败,例如 如果(response.status==ok) 然后重定向到下一个仪表板页面;否则再次要求提供凭据

【讨论】:

    猜你喜欢
    • 2019-01-10
    • 2021-05-09
    • 2020-09-26
    • 2017-08-31
    • 2019-07-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2021-02-08
    相关资源
    最近更新 更多