Skip to content
This repository has been archived by the owner on May 20, 2021. It is now read-only.

added support for object parameters #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions src/createBrowserApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
/* eslint-disable import/no-commonjs */
const queryString = require('query-string');

const getPathAndParamsFromLocation = location => {
const getPathAndParamsFromLocation = (location, paramsDeSerializer) => {
const path = encodeURI(location.pathname.substr(1));
const params = queryString.parse(location.search);
const params = paramsDeSerializer(location.search);
return { path, params };
};

Expand Down Expand Up @@ -50,9 +50,15 @@ function getHistory(history) {
return history || createBrowserHistory();
}

export default function createBrowserApp(App, { history: historyOption } = {}) {
export default function createBrowserApp(
App,
{ history: historyOption, paramsSerializer, paramsDeSerializer } = {
paramsSerializer: p => queryString.stringify(p),
paramsDeSerializer: p => queryString.parse(p),
}
) {
const history = getHistory(historyOption);
let currentPathAndParams = getPathAndParamsFromLocation(history.location);
let currentPathAndParams = getPathAndParamsFromLocation(history.location, paramsDeSerializer);
const initAction =
App.router.getActionForPathAndParams(
currentPathAndParams.path,
Expand All @@ -61,7 +67,7 @@ export default function createBrowserApp(App, { history: historyOption } = {}) {

const setHistoryListener = dispatch => {
history.listen(location => {
const pathAndParams = getPathAndParamsFromLocation(location);
const pathAndParams = getPathAndParamsFromLocation(location, paramsDeSerializer);
if (matchPathAndParams(pathAndParams, currentPathAndParams)) {
return;
}
Expand Down Expand Up @@ -155,7 +161,7 @@ export default function createBrowserApp(App, { history: historyOption } = {}) {
) {
currentPathAndParams = pathAndParams;
history.push(
`/${pathAndParams.path}?${queryString.stringify(
`/${pathAndParams.path}?${paramsSerializer(
pathAndParams.params
)}`
);
Expand Down