import { useNavigate } from 'react-router-dom';
/**
* BackButton component that navigates back in history or to a fallback route.
*/
function BackButton() {
// Get the navigate function from react-router-dom
const navigate = useNavigate();
const handleBack = () => {
if (window.history.length > 2) {
navigate(-1);
} else {
navigate('/memos'); // fallback route
}
};
return (
<button
onClick={handleBack}
className="px-4 py-2 text-sm font-medium rounded border border-blue-600 text-blue-600 hover:bg-blue-50"
>
Back
</button>
);
}
export default BackButton;
Source