How can I render mobile and desktop specific assets within a component by calculating the page width. Seems like ternary expressions are the way by calculating window width but I'm running into issues.
Here is the condition:
If the window size is greater than lets say 500 - render the desktop component which as desktop specific assets.
Else, render the mobile component which has mobile specific assets.
Another case in this logic is if a video url has been provide this takes priority over the fallback image.
Here is what I have so far, I have a resize function to get the "window.innerWidth" and update state like so:
resize() {
let windowWidth = window.innerWidth;
this.setState({
windowWidth: windowWidth
})
}
I pass the variable windowWidth as props to the component with the conditional logic.
Here is my logic so far:
render() {
const {windowWidth, video, fallbackImage, mobileVideo, mobileImage, videoCallback, videoAnimationOffset} = this.props;
return (
<div className="hero-wrap col-xs-12 no-padding">
{/* If video is available load otherwise render fallback image */}
{video ? (
<div className="video-container">
<Video
video={video}
mobileVideo={mobileVideo}
videoCallback={videoCallback}
videoAnimationOffset={videoAnimationOffset}
/>
</div>
) :
<div className="image-container" style={{backgroundImage: `url(${process.env.DB_URL}${fallbackImage})`}}>
</div>
}
</div>
)
}
Here is maybe a concept of what I think could work but you can't nest ternary expressions:
{/* If window width is greater than 500 render desktop */}
{windowWidth > 500 ?
{video ? (
<div className="video-container">
<Video
video={video}
mobileVideo={mobileVideo}
videoCallback={videoCallback}
videoAnimationOffset={videoAnimationOffset}
/>
</div>
) :
<div className="image-container" style={{backgroundImage: `url(${process.env.DB_URL}${fallbackImage})`}}>
</div>
} : null
}