Support for parsing and serializing URL queries is provided by the useQueries
enhancer function. Simply use a wrapped version of your createHistory
function to create your history
object and you'll have a parsed location.query
object inside listen
and listenBefore
callbacks.
import { createHistory, useQueries } from 'history'
// Use the built-in query parsing/serialization.
let history = useQueries(createHistory)()
// Use custom query parsing/serialization.
let history = useQueries(createHistory)({
parseQueryString: function (queryString) {
return qs.parse(queryString)
},
stringifyQuery: function (query) {
return qs.stringify(query, { arrayFormat: 'brackets' })
}
})
history.listen(function (location) {
console.log(location.query)
})
Query-enhanced histories accept URL queries as trailing arguments to createPath
, and createHref
, and accept query
as a key for push
and replace
.
history.createPath('/the/path', { the: 'query' })
history.push({ pathname: '/the/path', query: { the: 'query' } })