Skip to content

Commit

Permalink
useResponsiveValue: Fires a side effect on dependency change
Browse files Browse the repository at this point in the history
The useResponsiveValue hook does not update the value if one of the dependencies changes.
Internationalisation is a good example for this situation.
  • Loading branch information
hallaji authored and kettanaito committed Aug 4, 2020
1 parent bf10bb0 commit 353843f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/atomic-layout/src/hooks/useResponsiveValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useState, useEffect } from 'react'
import { withBreakpoints } from '@atomic-layout/core'
import useBreakpointChange from './useBreakpointChange'

Expand All @@ -13,10 +13,14 @@ const useResponsiveValue = <ValueType>(
): ValueType => {
const [value, setValue] = useState<ValueType>(defaultValue)

useBreakpointChange(() => {
const callback = () => {
const nextValue = withBreakpoints<ValueType>(breakpoints, defaultValue)
setValue(nextValue)
})
}

useEffect(callback, [breakpoints, defaultValue])

useBreakpointChange(callback)

return value
}
Expand Down

0 comments on commit 353843f

Please sign in to comment.