Skip to content

Leverage Arrow functions. Browser history nav button issue #5606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Intercepting Query String Changes in Web Parts
description: Building web parts that respond to query string changes.
ms.author: bekaise
ms.date: 01/28/2020
ms.date: 04/22/2020
ms.prod: sharepoint
localization_priority: Normal
---
Expand All @@ -16,7 +16,7 @@ First let's assume you have a basic web part class that renders the current quer
```typescript
export default class HelloWorldWebPart extends BaseClientSideWebPart<IHelloWorldWebPartProps> {
public render(): void {
this.domElement.innerHTML = window.location.query;
this.domElement.innerHTML = window.location.search;
}
}
```
Expand All @@ -26,28 +26,30 @@ We can use the following changes to trigger our web part to re-render in the eve
```typescript
export default class HelloWorldWebPart extends BaseClientSideWebPart<IHelloWorldWebPartProps> {
public onInit(): Promise<void> {
(function (history) {
((history) => {
var pushState = history.pushState;
history.pushState = function (state, key, path) {
this._onUrlChange();
return pushState.apply(history, arguments);
history.pushState = (state, key, path) => {
pushState.apply(history, [state, key, path]);
this._onUrlChange(path);
};
})(window.history);

window.addEventListener('popstate', function (e) {
this._onUrlChange();
//Currently browsing by the browser history buttons ( back / forward ) doesnt cause any effect on a sp conditionally loaded page.
//this._onUrlChange();
});

return Promise.resolve();
}

public render(): void {
this.domElement.innerHTML = window.location.query;
this.domElement.innerHTML = window.location.search;
}

private _onUrlChange(): void {
private _onUrlChange(newpath = window.location.search): void {
// any logic you might want to trigger when the query string updates
// e.g. fetching data
// e.g. logging the URL changes // console.log(newpath)
this.render();
}
}
Expand All @@ -56,4 +58,4 @@ export default class HelloWorldWebPart extends BaseClientSideWebPart<IHelloWorld
There are two parts to what we are doing in onInit:

1. Ensuring that whenever pushState is called by other javascript we trigger our `_onUrlChange` method.
1. Ensuring that whenever the browser fires a 'popstate' event (for example, user pressing the browser back button) we trigger our `_onUrlChange` method.
1. Ensuring that whenever the browser fires a 'popstate' event (for example, user pressing the browser back button) we trigger our `_onUrlChange` method.