so I have a bug that I can't seem to solve. Currently I am using react with react router, however I cannot seem to get react to redirect to a page. Here is a expert from my code (keep in mind this is a functional component)
<Route path="/login">
<Login
onSubmit={(user) => {
login(user, (status) => {
if (status == 200) {
history.push('/home');
} else {
history.push('/login');
}
});
}}
/>
</Route>
(For reference, I have a login component which takes in a onSubmit callback and in which I call a predefined login method in check the status of the response from my backend and redirect accordingly)
When I run this code and redirect, I get a error that says
Unhandled Rejection (TypeError): Cannot read property 'push' of undefined
And just to be clear, I have defined history in the first few lines of my component
let history = useHistory();
Any help will be greatly appreciated!
history
is resolving toundefined
so you are trying to invokeundefined.push('path')
, hence the error. You need to makehistory
accessible within the scope of the component like this: