gpt4 book ai didi

javascript - 按下提交按钮时如何防止 React 回发

转载 作者:行者123 更新时间:2023-11-30 13:56:02 26 4
gpt4 key购买 nike

在 React 组件中如何防止它回发,以便在按下提交按钮时页面不会重新加载。

组件代码如下:

const class MyComponent extends React.Component<IEditCampaignStateProps & IEditCampaignDispatchProps, EditCampaignState> {

...

onSubmit = (e: any) => {
const campaign: Campaign = {
title: this.state.campaign.title,
}
this.props.addCampaign(campaign);
}

render() {
return (
<Container>
...
</Container>
)
}
}

最佳答案

尝试在您的代码中添加一行...

 onSubmit = (e: any) => {
e.preventDefault();
const campaign: Campaign = {
title: this.state.campaign.title,
}
this.props.addCampaign(campaign);
}

我已经添加了.. e.preventDefault();

关于javascript - 按下提交按钮时如何防止 React 回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57284089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com