idk why but my useEffect happens twice and its messing up my code :(
useEffect(() => {
axios
.post("/register", {
fullname,
username,
email,
password,
})
.then((response) => {
setMessage(JSON.stringify(response.data.success));
console.log(message);
if (message === "true") {
setOpen(true);
setTimeout(() => {
navigate("/signin");
}, 3000);
} else {
setErrorMessage(
JSON.stringify(Object.keys(response.data.msg.keyPattern)[0])
); //TODO: create better validation message
}
})
.catch((error) => {
return error;
});
}, [email, fullname, message, navigate, password, username]);
im kinda new to API calls. what im trying to achive is getting a response from the server with the success msg, if its true there is snackbar that i want to open and then navigate to signin. if the success msg is false i want to alert an error.
another problem that i think is happening because of the useEffect is that even thought i have email property and user property that are unique, it saves in the database more than once.
app.js file -
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();