-4

I'm trying to implement a signup form and using the react-toastify package to show success and error messages. While the toast.error function works perfectly, the toast.success function does not display the success message upon successful form submission. Here's the relevant code for my Signup component:

import axios from "axios";
import { useState } from "react";
import { toast, ToastContainer } from "react-toastify";
// import "../signup/signup.css";
import { Link, useNavigate } from "react-router-dom";
import "react-toastify/dist/ReactToastify.css";

const Signup = () => {
  const navigate = useNavigate();

  const [formData, setFormData] = useState({
    name: "",
    phone: "",
    email: "",
    password: "",
  });

  const handleChange = (e) => {
    const { name, value } = e.target;
    setFormData((prevState) => ({
      ...prevState,
      [name]: value,
    }));
  };

  const handleSignup = async (e) => {
    e.preventDefault();

    if (
      !formData.name ||
      !formData.phone ||
      !formData.email ||
      !formData.password
    ) {
      toast.error("Please fill in all fields.", {
        position: "top-right",
        autoClose: 3000,
      });
      return;
    }

    try {
      const res = await axios.post("http://localhost:0000/example", formData);
      console.log("Response from server:", res.data);

      toast.success("Form Submitted Successfully", {
        position: "top-right",
        autoClose: 5000,
      });

      setFormData({
        name: "",
        phone: "",
        email: "",
        password: "",
      });

      setTimeout(() => {
        navigate("/signin");
      }, 5000);
    } catch (error) {
      toast.error("Something went wrong. Please try again.", {
        position: "top-right",
        autoClose: 5000,
      });
    }
  };

  return (
    <div className="flex items-center justify-center min-h-screen bg-gray-100">
      <ToastContainer />
      <div className="bg-white shadow-lg rounded-lg w-full max-w-lg p-8">
        <h2 className="text-lg font-bold text-center mb-6">Create Account</h2>
        <form onSubmit={handleSignup}>
          <div className="mb-4">
            <input
              type="text"
              className="w-full border border-gray-300 rounded-md px-4 py-2"
              name="name"
              placeholder="Enter Full Name"
              value={formData.name}
              onChange={handleChange}
            />
          </div>
          <div className="mb-4">
            <input
              type="text"
              className="w-full border border-gray-300 rounded-md px-4 py-2"
              name="phone"
              placeholder="Enter Phone"
              value={formData.phone}
              onChange={handleChange}
            />
          </div>
          <div className="mb-4">
            <input
              type="email"
              className="w-full border border-gray-300 rounded-md px-4 py-2"
              placeholder="Enter Email"
              name="email"
              value={formData.email}
              onChange={handleChange}
            />
          </div>
          <div className="mb-6">
            <input
              type="password"
              className="w-full border border-gray-300 rounded-md px-4 py-2"
              placeholder="Enter Password"
              name="password"
              value={formData.password}
              onChange={handleChange}
            />
          </div>
          <div>
            <input
              type="submit"
              className="w-full bg-blue-600 text-white rounded-md py-2 cursor-pointer"
              value="Signup"
            />
          </div>
          <div className="text-center mt-4">
            <span>
              Already have an account?
              <Link to="/signin" className="text-blue-600 ps-1">
                Login Here
              </Link>
            </span>
          </div>
        </form>
      </div>
    </div>
  );
};

export default Signup;

9
  • Can you mention the react-toastify's version ? Commented Jul 29 at 12:09
  • React-toastify version is: 11.0.5, it working properly in signin form but not working properly in signup even toast.error working fine only toast.success not working Commented Jul 29 at 12:29
  • 1
    in an effort to gather data, could you move the toast.success call before your api call? or even before the try/catch block? does it display then? what if you leave it after the api call, but change it to a toast.error call? Commented Jul 29 at 13:35
  • 1
    is the catch block being run when you call handleSignup? is the console.log being run? it's starting to sound like the api call or how the response is handled may be the issue. Commented Jul 29 at 14:22
  • 1
    if the catch block is run, that means something in the try block is throwing an exception before it is able to call toast.success if the api call is working, perhaps it is returning null, and the console.log is attempting to access the data property from null. instead of the toast.error in your catch block, simply do a console.error of the exception. it will likely be clear what the issue is. Commented Jul 29 at 16:57

1 Answer 1

1

I've copied your code and tried to make it work and it worked well.

Link - is the source which I've used. Can you check the code, and review your code again.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, i have checked it it is working but when i adding this in my project it is not working. Can you imagine what can be possible problem in my project even signin page working fine.
Did you check your css imports?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.