Skip to content

Commit 6aea0e9

Browse files
committed
added push method
1 parent 653c81e commit 6aea0e9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/browser-router.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import Router, { initParams, initResult } from './router';
3-
import { Context } from 'router-async';
3+
import { Context, stringifyQuery } from 'router-async';
44

55
export default class BrowserRouter extends Router {
66
private history: any;
@@ -40,6 +40,16 @@ export default class BrowserRouter extends Router {
4040
}
4141
}
4242
}
43+
async push(path) {
44+
// console.warn('Please use navigate method instead of push, it will be deprecated in future');
45+
if (typeof path === 'string') {
46+
await this.navigate(path);
47+
} else {
48+
let fullPath = path.pathname;
49+
if (path.query) fullPath += `?${stringifyQuery(path.query)}`;
50+
await this.navigate(fullPath);
51+
}
52+
}
4353
// TODO: maybe we need to make this history methods works through navigate?
4454
goBack() {
4555
this.history.goBack();
@@ -50,9 +60,10 @@ export default class BrowserRouter extends Router {
5060
go(n) {
5161
this.history.go(n);
5262
}
53-
private _locationChanged = async ({ pathname }) => {
63+
private _locationChanged = async ({ pathname, hash, search }) => {
64+
const path = pathname + search + hash;
5465
try {
55-
const { path, location, route, status, params, redirect, result, ctx } = await this.router.run({ path: pathname });
66+
const { location, route, status, params, redirect, result, ctx } = await this.router.run({ path });
5667
const props = {
5768
router: {
5869
path,

0 commit comments

Comments
 (0)