Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta components #729

Closed
rauchg opened this issue Jan 10, 2017 · 2 comments
Closed

Meta components #729

rauchg opened this issue Jan 10, 2017 · 2 comments

Comments

@rauchg
Copy link
Member

rauchg commented Jan 10, 2017

Just saw this on Twitter

Basically shows how much code is needed to make something as simple as an image work well on a huge variety of devices.

And for more context, you can read a conversation in the styled jsx repo as far as internationalization: vercel/styled-jsx#74

Or you could imagine a certain component within your system having different capabilities based on the authenticated state of the user. Imagine a /dashboard component whose code is different based on the role.

The bottom line is: don't ship more code than is necessary. That means cutting down on the number of logical branches in the components code. For example, by doing ahead-of-time compilation.

If the user is on iOS, give them a certain <img> tag, and if the user is on web, a different one. The render code should, in those cases, completely exclude the possibility that you'll switch from iOS to Web (because it's impossible), so the code should not even reach the user.

With our new programmatic API, you can solve the problem at the page level. A certain route /dashboard can resolve to different components by examining the req object.

But how do we solve it for nested components? Imagine that you use an <Image/> component that you import, that you also want to compile ahead of time according to the characteristics of the request.

Perhaps the solution is #728: dynamic imports, in combination with getInitialProps:

import parse from 'parse-user-agent'
import React from 'react'
export default class extends React.Component {
  static getInitialProps ({ req }) {
    const isIOS = parse(req ? req.headers['User-Agent'] : navigator.userAgent).ios
    if (isIOS) {
      Image = import('./image-ios')
    } else { 
      Image = import('./image-web')    
    }
    return { Image: await Image }
  }

  render () {
    const { Image } = this.props
    return <div><Image src="doge.png" alt="wow" /></div>
  }
}

Anyway, would like to hear your thoughts!

@sedubois
Copy link
Contributor

This is the twitter link: https://twitter.com/thomasfuchs/status/761153909738115072

@timneutkens
Copy link
Member

Closing this cause of inactivity. Dynamic imports are in beta, rauchg's proposed solution checking the user agent could be combined with https://github.com/quentin-sommer/react-useragent makes it pretty cool 👌

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants