Skip to content

Commit

Permalink
docs(website): fix query in SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Dec 26, 2019
1 parent 3d56b1a commit 9222c79
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions website/src/components/playground/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React from 'react'
import qs from 'query-string'
import { createBrowserHistory } from 'history'

const history = createBrowserHistory()

function formatQuery(state, initialState) {
const lightState = Object.entries(state).reduce(
(lightState, [key, value]) => {
Expand All @@ -26,7 +24,19 @@ function getLocation() {
return window.location
}

let browserHistory

function useHistory() {
if (typeof window === 'undefined') return null
if (browserHistory) {
return browserHistory
}
browserHistory = createBrowserHistory()
return browserHistory
}

function useLocation() {
const history = useHistory()
const [location, setLocation] = React.useState(getLocation)
React.useEffect(() => {
return history.listen(location => setLocation(location))
Expand All @@ -35,6 +45,7 @@ function useLocation() {
}

export function useQuery(getInitialState = {}) {
const history = useHistory()
const [initialState] = React.useState(getInitialState)
const location = useLocation()
const locationRef = React.useRef(location)
Expand Down

1 comment on commit 9222c79

@vercel
Copy link

@vercel vercel bot commented on 9222c79 Dec 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.