Skip to content
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

Add ability to pass in custom getLocation 🥳 #239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/lib/history.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
let getLocation = source => {
let defaultGetLocation = source => {
return {
...source.location,
state: source.history.state,
key: (source.history.state && source.history.state.key) || "initial"
};
};

let createHistory = (source, options) => {
let createHistory = (source, options = {}) => {
let { getLocation = defaultGetLocation } = options;

let listeners = [];
let location = getLocation(source);
let transitioning = false;
Expand Down
28 changes: 28 additions & 0 deletions src/lib/history.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createHistory, createMemorySource } from "./history";

describe("createHistory", () => {
test("should use default getLocation if not passed in through options", () => {
let history = createHistory(createMemorySource());
expect(history.location).toHaveProperty("pathname", "/");
});

test("should use custom getLocation", () => {
let options = {
getLocation(source) {
const params = new URLSearchParams(source.location.search);

return {
...source.location,
query: params,
state: window.history.state,
key: (window.history.state && window.history.state.key) || "initial"
};
}
};
let history = createHistory(createMemorySource("/"), options);
history.navigate("?yo=dawg");

expect(history.location.query).toBeInstanceOf(URLSearchParams);
expect(history.location.query.get("yo")).toEqual("dawg");
});
});
30 changes: 30 additions & 0 deletions website/src/markdown/api/createHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,33 @@ let history = createHistory(window)
let source = createMemorySource("/starting/url")
let history = createHistory(source)
```

There may be times within your screen where you may need to change a part of the UI based on some query param. By default we do not parse the search property of the source `location`. To do so we allow you to pass in a custom `getLocation` function into the options object. For example:

```jsx
import {
createMemorySource,
createHistory
} from "@reach/router"

let options = {
getLocation(source) {
const query = new URLSearchParams(
source.location.search
)

return {
...source.location,
query,
state: source.history.state,
key:
(source.history.state &&
source.history.state.key) ||
"initial"
}
}
}

// listen to the browser history
let history = createHistory(window, options)
```