The `history` library provides a robust and environment-agnostic API for managing session history in JavaScript applications, abstracting away the complexities of different platforms (browser, hash, memory). It allows developers to manage the history stack, navigate programmatically, and persist state across sessions. The current stable version is 5.3.0. Releases are generally aligned with React Router versions, as it's a core dependency for React Router v6. Key differentiators include its pluggable architecture for different history implementations (browser, hash, memory) and its strong TypeScript support, making it suitable for both web applications and server-side rendering or testing environments. It offers a consistent API regardless of the underlying history mechanism, simplifying routing logic in SPAs.
npm install historyVerified import paths — ran on the pinned version, not inferred.
Demonstrates creating browser, hash, and memory history instances, listening for location changes, programmatic navigation (push/replace), and using the navigation blocking API with cleanup.
Update your bundler configuration (e.g., Webpack, Rollup, Vite) to correctly handle native ESM modules. For Node.js, ensure you are using a version that supports `type: 'module'` in `package.json` or `.mjs` extensions, and prefer ESM `import` statements.
Review your TypeScript codebase for usages of `State`, `PartialPath`, and `PartialLocation`. Update `State` handling to incorporate type guards or assertions to narrow the `unknown` type. Replace `PartialPath` with `Partial<Path>` and `PartialLocation` with `Partial<Location>`.
If you relied on `Location<T>` for strong type-checking of `location.state`, you will need to adapt. For versions 5.1.0 and later, `location.state` will typically be `any` or `unknown`, requiring runtime type checks, explicit casting, or use of the exported `State` type with manual narrowing.
If you are using `history` v5 or newer, uninstall `@types/history` from your project: `npm uninstall @types/history` or `yarn remove @types/history`. Ensure your `tsconfig.json` is correctly configured to pick up the built-in types.
Ensure your project is configured for native ESM if you're using `import` statements. If using `require()`, adapt to `const { createBrowserHistory } = require('history');` for named exports, or configure your bundler to handle ESM interop correctly, especially in Node.js environments with `type: 'module'`.For `history` v5, `location.state` is typed as `any` or `unknown`. Access `location.state` directly, and apply runtime type narrowing or explicit type assertions if you expect a specific type, e.g., `const myState = history.location.state as { from: string };` or `if (typeof history.location.state === 'object' && history.location.state !== null) { /* safe access */ }`.For `history` v5 and newer, types are bundled. Ensure `history` is correctly installed via `npm install history`. If on `history` v4, install `@types/history` (`npm install --save-dev @types/history`). If on `history` v5 and still seeing this, ensure you have removed `@types/history`.
No dependency data recorded yet.