Skip to content

Commit

Permalink
Merge pull request #626 from vtex-apps/fix/device-rerender
Browse files Browse the repository at this point in the history
Fix/device rerender
  • Loading branch information
lbebber authored Sep 29, 2021
2 parents a522699 + f93df0a commit eb1b449
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Prevents unnecessary rerender on `withDevice` HOC.

## [8.132.1] - 2021-09-28
### Changed
Expand Down
17 changes: 16 additions & 1 deletion react/utils/withDevice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const useDevice = (hints: RenderRuntime['hints']) => {
/** These screensizes are hardcoded, based on the default
* Tachyons breakpoints. They should probably be the ones
* configured via the style.json file, if available. */

const isScreenMedium = useMediaLayout({ minWidth: '40rem' })
const isScreenLarge = useMediaLayout({ minWidth: '64.1rem' })

Expand Down Expand Up @@ -56,11 +57,25 @@ const useDevice = (hints: RenderRuntime['hints']) => {
export const withDevice = <P extends Props>(
Component: ComponentType<P & WithDeviceProps>
): FC<P> => {
const MemoizedWithDevice = React.memo(
({ type, isMobile, ...props }: DeviceInfo & Props) => (
<Component deviceInfo={{ type, isMobile }} {...(props as P)} />
)
)

MemoizedWithDevice.displayName = 'MemoizedWithDevice'

const WithDevice = ({ ...props }: P) => {
const hints = props.runtime.hints
const deviceInfo = useDevice(hints)

return <Component deviceInfo={deviceInfo} {...(props as P)} />
return (
<MemoizedWithDevice
type={deviceInfo.type}
isMobile={deviceInfo.isMobile}
{...props}
/>
)
}

return WithDevice
Expand Down

0 comments on commit eb1b449

Please sign in to comment.