I'm trying to use Link, from React Router, to make a button take the user to another page in the app.
However, the button should trigger an HTTP POST request through a handler function and only then take the user to this other page, provided there are no errors.
This is my Link JSX element:
<Link to="/next-page">
<Button onClick={e => { handleSubmit(e) }}>Go</Button>
</Link>
And this is the handler function:
const handleSubmit = (e) => {
e.preventDefault();
axios.post('http://localhost:8000/page', pageData)
.then(res => console.log(res.data))
.catch(err => console.log(err));
}
The current behavior is that the HTTP request works fine, but the button doesn't take the user to the desired page (even though the link address is apparently present, which I confirmed by inspecting the button in the browser).
Does the fact that axios.post() returns a promise breaks the expected behavior?
How should I deal with it?