Skip to content

Commit

Permalink
fix(ssr): temp solution to allow ssr till v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Vincze committed Feb 29, 2020
1 parent 110e958 commit 4e1e5a9
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/useBreakpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ export const calculateValue: TCalculateValue = function(defaultValue, breakpoint
return defaultValue
}

let cachedIw = window?.innerWidth || global?.innerWidth
const getInnerWidth = () => typeof window !== 'undefined' ? window.innerWidth : (global.innerWidth || 1920)

let cachedIw = getInnerWidth()

export function useBreakpoint(defaultValue: any, breakpointValues: any[]): any
export function useBreakpoint(defaultValue: undefined, breakpointValues: undefined): { [key: string]: boolean }
export function useBreakpoint(defaultValue, breakpointValues) {
const [innerWidth, setInnerWidth] = useState(cachedIw)
useResize(() => {
cachedIw = window?.innerWidth || global?.innerWidth
cachedIw = getInnerWidth()
setInnerWidth(cachedIw)
})
return useMemo(() => calculateValue(defaultValue, breakpointValues, innerWidth), [innerWidth, defaultValue])
Expand Down

0 comments on commit 4e1e5a9

Please sign in to comment.