how to use in Reactjs functional component history.push
Instead of history.push
try to use navigate
import { useNavigate } from 'react-router-dom';
const addToCartHandler = () => {
navigate(`/cart/${productId}?qty=${qty}`)
};
I think you handle routing with react-router
. If so, you need to use the history object which is passed through the ReactRouterContext
.
To do that, you need to use useHistory
hook to get the history
object.
// ...
import { useHistory } from "react-router";
// ...
function ProfileForm(props) {
const history = useHistory();
const onSubmit = (data, e) => {
e.target.reset();
history.push({
pathname: "/OnSubmit",
state: {
response: messageFromServer
}
});
}
}