React Core & Hooks (407 Qs)•Part 2: React Router & Navigation (78 - 88)
How React Router is different from history library?
mid LevelCode ExampleRouting
React Router is a wrapper around the history library which handles interaction with the browser's window.history with its browser and hash histories. It also provides memory history which is useful for environments that don't have global history, such as mobile app development (React Native) and unit testing with Node.
Practical Code Example
jsx
// 'history' is a low-level library managing browser history stack.
// 'React Router' wraps history and provides declarative components (<Routes>, <Route>, <Link>):
import { useNavigate } from 'react-router-dom';
function LogoutButton() {
const navigate = useNavigate();
return <button onClick={() => navigate('/login')}>Logout</button>;
}