I would want load new component after server response using React. Its could be a simple operation but something does not working at the moment. My code is the following:
const formSubmit = async e => {
e.preventDefault();
const response = await dataSubmit({
toSend
});
if(response.status === 200){
console.log("status 200");
return(
<>
<ComponentPage />
</>
);
}
else{
console.log("error status");
return(
<>
<ComponentPage />
</>
);
}
}
async function dataSubmit(toSend) {
return fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(toSend)
})
.then(data => (data.json()))
}
I tried removing e.preventDefault()
or using effect but is not working. This component will have to ovverrife atually Form component shown. console.log
lines works. How can I solve? Thanks