Created
May 29, 2021 01:57
-
-
Save rw3iss/6a81b70ead292a76cd028bcbf418d5ce to your computer and use it in GitHub Desktop.
MobileComponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState } from 'react'; | |
import { isMobile } from 'lib/utils/UiUtils'; | |
// Dynamic component which rendered a different child depending on mobile vs. desktop. | |
// Will re-render upon resize state change. | |
export default MobileComponent = ({ component: Component, ...props }) => { | |
const [currIsMobile, setIsMobile] = useState(isMobile()); | |
useEffect(() => { | |
window.addEventListener('resize', (e) => { | |
let _isMobile = isMobile(); | |
if (currIsMobile != _isMobile) { | |
setIsMobile(_isMobile) | |
} | |
}); | |
}) | |
if (currIsMobile) { | |
return <props.mobile {...props} /> | |
} else { | |
return <props.normal {...props} /> | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment