简体   繁体   English

如果所有验证都正确,则使用React.JS Link Tag渲染下一个组件

[英]Rendering the Next Component if all the validations are correct with React.JS Link Tag

I have a button on click of which i go to the next page , ie 我有一个单击按钮,可以转到下一页,即

<Link to="/results"><button>Calculate</button></Link>

But i want to go the next page , only if few validations or a flag showResults is true . 但是,仅当很少进行验证或将showResults标志设为true时,我才想转到下一页。 And, i dont want to hide the button also . 而且,我也不想隐藏该按钮。 Button should be there , but it should go to the next page only if the validations are correct. Button应该在那里,但是只有在验证正确的情况下,它才应该进入下一页。

I'm assuming you want to update the path of your link. 我假设您要更新链接的路径。

You could do something like this: 您可以执行以下操作:

render(){
    const toLink = condition ? '/' : '/results'
    return(
        <Link to={toLink}><button>Calculate</button></Link>
    )

}

You can add preventDefault() 您可以添加preventDefault()

render(){
    return(
        <Link to={toLink} onClick={(e) => {if (!showResults) {e.preventDefault();}}}>
            <button>Calculate</button>
        </Link>
    )

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM