reactjs - 已成功提交有关 React 的警报
问题描述
如何在 React 上获得成功提交的警报
嗨,我是 React 新手,我创建了一个联系我们页面,所以当用户单击提交按钮时我想显示一条消息,提交成功我不知道该怎么做?谁能帮我解决这个问题,拜托
#这是我的代码主体
import React, { Component } from "react";
import axios from "axios";
class ContactUS extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
email: "",
contact: "",
message: "",
};
}
handleData = (e) => {
this.setState({
[e.target.name]: e.target.value,
});
};
handleSubmit = (e) => {
e.preventDefault();
console.log(this.state);
// conditions
) {
axios
.post(API_BASE_URL + "contactus/", {
name: this.state.name,
email: this.state.email,
contact: this.state.contact,
message: this.state.message,
})
.then((response) => {
this.setState({
resData: response.data,
});
this.setState({
name: "",
email: "",
contact: "",
message: "",
});
}
})
.catch(function (error) {
console.log(error.response);
});
} else {
this.setState({
resData: "Oops! Something went wrong!",
});
}
}, );
};
解决方案
添加
this.state = {
name: "",
email: "",
contact: "",
message: "",
success : false
};
如果成功
this.setState({ success: true});
在渲染中
{this.state.success ?
"success" : "awaiting"
}
推荐阅读
- javascript - FeathersJS 中的非 CRUD 路由
- alexa-skills-kit - 如何使用alexa和对话流以相同的意图获取用户不知道的两个不同文本
- java - 如何通过一些自动颜色调整将我的 Android Studio 颜色从黑色更改回默认值
- java - 使用 javac -cp 将 Jar 文件添加到 java 程序不起作用
- python - 'builtin_function_or_method' 对象不可下标。" 5
- zend-framework3 - Zend Framework 3 从工厂重定向
- ruby-on-rails - Rails rake db 未初始化常量 Faker::User
- visual-studio-2017 - 发现 XUnit 测试,将在所有测试中运行,但不在播放列表中运行
- postgresql - 如何使用 pgpool 主从锁定数据库行?
- asp.net-web-api - 我可以将 FlurlClient 与 Asp.Net Core TestServer 一起使用吗?