Skip to content

Commit 3cdf432

Browse files
authored
Add back support (#140)
* Add back suppor * Add forward support * Add readme entry
2 parents daa9bc1 + 8d6d33a commit 3cdf432

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ Returns an object with the following properties:
292292
- `path: string` - The current path
293293
- `query: Record<string, string>` - The current query string parameters (`/profile?name=John` -> `{ name: 'John' }`)
294294
- `route: (url: string, replace?: boolean) => void` - A function to programmatically navigate to a new route. The `replace` param can optionally be used to overwrite history, navigating them away without keeping the current location in the history stack.
295+
- `back: () => void` - A function to programmatically navigate back one entry in the browser history stack.
296+
- `forward: () => void` - A function to programmatically navigate forward one entry in the browser history stack.
295297

296298
### `useRoute`
297299

src/router.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ interface LocationHook {
4343
path: string;
4444
query: Record<string, string>;
4545
route: (url: string, replace?: boolean) => void;
46+
back: () => void;
47+
forward: () => void;
4648
}
4749
export const useLocation: () => LocationHook;
4850

src/router.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function handleNav(state, action) {
6161

6262
if (push === true) history.pushState(null, '', url);
6363
else if (push === false) history.replaceState(null, '', url);
64+
6465
return url;
6566
};
6667

@@ -112,6 +113,12 @@ export function LocationProvider(props) {
112113
path,
113114
query: Object.fromEntries(u.searchParams),
114115
route: (url, replace) => route({ url, replace }),
116+
back: () => {
117+
history.back();
118+
},
119+
forward: () => {
120+
history.forward();
121+
},
115122
wasPush
116123
};
117124
}, [url]);

0 commit comments

Comments
 (0)