Heres my component code
constructor(props) {
super(props);
this.updateView = this.updateView.bind(this);
this.state = {
screen_width: ''
}
}
componentDidMount() {
window.addEventListener("resize", this.updateView);
this.setState({
screen_width: window.innerWidth
})
}
componentDidUpdate(prevProps) {
}
updateView() {
this.setState({
screen_width: window.innerWidth
})
}
displayHome() {
console.log(this.state.screen_width);
if (this.state.screen_width >= 768) {
return (
<React.Fragment>
<HomeDesktop/>
</React.Fragment>
)
}
else {
return (
<React.Fragment>
<HomeMobile/>
</React.Fragment>
)
}
}
render() {
return (
{this.displayHome()}
)
}
My problem is that on mobile devices, its supposed to display HomeMobile initially, but it just doesn't do that. The space that HomeMobile is supposed to take up is just blank. A picture is supposed to come up, but currently ONLY after scrolling does the picture show up. Initially I thought this was based on css positioning, but when I just put within render, the picture shows up as normal. This means for some reason displayHome() is not displaying the mobile component properly on the initial render.
My code works fine on desktop, if you are above a certain width, it will show the desktop view, and under a certain width it will show the mobile view. On mobile devices, the Home components just straight up don't show up.
Has anyone ever dealt with a similar issue with mobile views on the initial render? How do I get HomeMobile to show up on the initial render?