jquery 如何在Reactjs中检查有效的电子邮件地址

z9gpfhce  于 2022-11-29  发布在  jQuery
关注(0)|答案(1)|浏览(165)

我正在使用React js和next js,我试图在nextjs中处理“Axios response”,但我得到了“[object Object]”作为响应,我如何才能得到响应和使用条件(如果响应是...)?以下是我当前的代码

const handleSubmit = (e) => {
    e.preventDefault();
    
    const data = {
        name: state.name
      };                        
      
      setState({
        ...state,
        name: ""
      });

      axios.post('https://xxxxxxxxxxxxxxxx/', data).then(function (response) {
        console.log(response.data);
        //how to handle response
      });
};
xfb7svmp

xfb7svmp1#

import "./styles.css";
import { data } from "./data";
import { useState } from "react";

export default function App() {
  const [email, setEmail] = useState("");
  const [msg, setMsg] = useState("");

  function emailHandler(e) {
    e.preventDefault();
    if (email.length === 0) {
      setMsg("Please enter the mail");
    } else {
      const checkData = data.filter((item) => item.mail === email);
      checkData.length === 0
        ? setMsg("thanks for subscribing")
        : setMsg("Already subscribe");
    }
  }
  return (
    <div className="App">
      <form onSubmit={(e) => emailHandler(e)}>
        <input
          type="email"
          value={email}
          onChange={(e) => setEmail(e.target.value)}
        />
        <button>Subscribe</button>
      </form>
      {msg}
    </div>
  );
}

相关问题