Reactjs 根据onClick React js中的条件添加属性
如果条件为false,我想停止重定向当前页,因为如果条件失败,我想在同一页上显示错误消息,如果条件正确,我想重定向到另一页。我该怎么做Reactjs 根据onClick React js中的条件添加属性,reactjs,button,Reactjs,Button,如果条件为false,我想停止重定向当前页,因为如果条件失败,我想在同一页上显示错误消息,如果条件正确,我想重定向到另一页。我该怎么做 <Button fullWidth color="primary" onClick={function (){ if(condition) redirect to x; else redirect to y; }} /*to={setUrl}*/> <Trans>text</Trans> </Button>
<Button fullWidth color="primary" onClick={function (){
if(condition) redirect to x;
else
redirect to y;
}} /*to={setUrl}*/>
<Trans>text</Trans>
</Button>
如果您使用的是react路由器,则可以使用this.props.history.push'url重定向到url,并在showNotification中实现通知显示
你想停止重定向是什么意思?基本上,React是通过设置状态来工作的,在新渲染中,可以从当前状态设置属性。
<Button fullWidth color="primary" onClick={()=>{
if(condition) this.props.push('/home');
else
this.showNotification();
}}/>
<Trans>text</Trans>
</Button>