It is so that I get a problem like doing that when I write email for the first time and it is sent in the database. Then I expect something to come back. Which also do. But it adds nothing to my usestate at all. But when I try to click again and write email again. Then the message I expected is added to my usestate.
So after the second time I have clicked it works exactly as it should and I want them to but it does nothing the first time.
Here you can see my html part for the react.tsx file.
<form onSubmit={(e) => submitClick(e)}>
<label className='labelValue'>Email</label>
<input
type="text"
className='w-full border-2 border-slate-100 p-2 md:rounded-lg mt-1 md:mt-2'
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder='Email here' />
<button disabled={disabledNow} className='btn w-full'>
{disabledNow ? "Account are createde now." : "Create user"}
</button>
</form>
Here can u see my React for at post to database in my react.tsx file.
const [email, setEmail] = useState('');
const [dataValue, setDataValue] = useState([]);
const [item, setItem] = useState(0);
const [disabledNow, setDisabledNow] = useState(false);
const submitClick = (e) => {
e.preventDefault();
setItem(1);
setDisabledNow(true)
}
useEffect(() => {
const dataMail = {
Email: email
};
let ignore = false;
if (item === 1) {
if (!ignore) {
const fetchData = async () => {
const result = await axios.post("https://localhost:7176/api/user", dataMail)
const fetchItem = await result.data;
setDataValue(fetchItem);//not adding fetchItem first time i make a click onSubmit. But after this i get a fetchItem to setDataValue.
console.log(dataValue);
}
fetchData();
setEmail('');
setItem(0)
ignore = true
if (dataValue.length > 0) {
console.log("Out of Fetch now. HERE")
toast(dataValue[1], { duration: 7000 })
console.log("Notify now HERE")
}
}
}
}, [dataValue, email, item])
As I said, I have also tried to remove async and await and it still gives the same problem that I don’t have it on.