2

Which path should I choose for making an App which should have two different views. One for mobile and one for desktop and CSS Media Queries won't be enough to solve the indifference.

Any advice or guidance is appreciated. I have tried to research but with no luck so where does the documentation handle this, as well as other sources how this is dealt with.

I guess I could use vanilla JS and manipulate DOM directly but that pretty much takes away the purpose of React.

3
  • 1
    Have you tried googling "responsive react"? I get several results that look useful. And you can always check window.width in your render() method and act accordingly. Commented Sep 28, 2018 at 10:59
  • Actually, "react-responsive" package seems to be the most efficient approach for this scenario. Commented Oct 1, 2018 at 6:45
  • Final note: ideally, a website doesn't need a different HTML structure to be responsive. Using CSS media queries alone should suffice. Even if you need completely restructure something, you can always implement something twice and hide one version in turn. Commented Oct 1, 2018 at 10:55

2 Answers 2

1

Did you try something like react-device-detect ?

https://www.npmjs.com/package/react-device-detect

You could have two different layouts like this :

<BrowserView>
  <h1> This is rendered only in browser </h1>
</BrowserView>
<MobileView>
  <h1> This is rendered only on mobile </h1>
</MobileView>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I'm new to React so did not stumble over that package before! I do want to render the mobile version for desktop resolutions too though. But this will come in handy!
1

How about creating 2 render methods for your every components, one for mobile and one for desktop.

When a user enters your app, get its device info and assign global view state to 1 for mobile and 2 for desktop. After that, render your components according to your view state prop with basic conditional check.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { view: this.props.view }; // "1" or "2"
  }

  render() {
    if (this.state.view === "1") {
      return (
        // mobile ui
      );
    } else {
      return (
        // desktop ui
      );
    }
  }
}

You can find much more information about React conditional rendering at here.


One more idea: You can split your app into 2 different top components. If you are using react-router, who doesn't :), after you get user's device info, you can redirect them like this:

app.js

<Switch>
      <Route path="/" name="Redirect" component={Redirect}/>
      <Route path="/mobile" name="Redirect" component={Mobile}/>
      <Route path="/desktop" name="Redirect" component={Desktop}/>
</Switch>

Redirect Component

render() {
  if (getDeviceInfo === "1")
    return (<Redirect to={"/mobile"}/>);
  else
    return (<Redirect to={"/desktop"}/>);
}

With this way, you don't need to keep 2 different ui of your component.

4 Comments

This seems like a good approach and something like I was thinking of, wasnt sure if this was considered alright to do and best practice. It should do the job though! Thanks!
Also, I probably wasnt clear enough, that I also wanted the mobile version to kick in for desktop resolutions as well. But I can probably use the same approach and use the device width(window width) as conditional statement in that regard.
I'm not saying that this is the best practice, but in the short run, this approach works like a charm :) Also, i would like to mention that, as soon as your app starts to get bigger, your classes become a mess. Be careful and try to keep your components as small as you can.
One more idea: check my answer again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.