使用ReactJS时,如果渲染有组件消失



以下是问题:

我有一个带有if语句渲染的ReactJS应用程序。在从查询后得到对API的响应后,我需要渲染组件,但它出现了一秒钟,然后就消失了。问题出在哪里?非常感谢。

handleSend() {
try {
axios.post('/login', {  posted_data: this.state.value, 
window_size: this.state.range_val, 
sin: this.state.value1, 
sin_v: this.state.value2 }).
then(response => this.setState({ token: response.data['data'] }));

} catch (e) {
console.log(`😱 Axios request failed: ${e}`);
}
}  

render() {
return (
<div className = "MarkdownEditor">

<Form> 
...
<Button variant="primary" type="submit" onClick={this.handleSend}>
Найти
</Button>
{ this.state.token == "a" && 
<h2> {this.state.token} </h2>
} 
</Form>

</div>
);
}
}

如果Form组件表示一个实际的HTML表单元素,那么当按钮触发提交时,您需要防止默认行为。否则页面将立即重新加载,这将解释您正在消失的元素。

handleSend(event) {
event.preventDefault();
...
};

最新更新