score:7

Accepted answer

fouc

fouc - so called flash of unstyled content can be as very problematic as so many tries of solving this issue.

to the point

let's consider following configuration of routing (react-router):

...
<pagelayout>
  <switch>
    <route exact path='/' component={home} />
    <route exact path='/example' component={example} />
  <switch>
</pagelayout>
...

where pagelayout is a simple hoc, containing div wrapper with page-layout class and returning it's children.

now, let's focus on the component rendering based on route. usually you would use as component prop a react compoment. but in our case we need to get it dynamically, to apply feature which helps us to avoid fouc. so our code will look like this:

import asyncroute from './asyncroute'

const home = asyncroute(() => import('./home'))
const example = asyncroute(() => import('./example'))

...

<pagelayout>
  <switch>
    <route exact path='/' component={home} />
    <route exact path='/example' component={example} />
  <switch>
</pagelayout>

...

to clarify let's also show how asyncroute.js module looks like:

import react, { component } from 'react'
import proptypes from 'prop-types'

import loader from 'components/loader'

class asyncimport extends component {
  static proptypes = {
    load: proptypes.func.isrequired,
    children: proptypes.node.isrequired
  }

  state = {
    component: null
  }

  togglefoucclass () {
    const root = document.getelementbyid('react-app')
    if (root.hasclass('fouc')) {
      root.removeclass('fouc')
    } else {
      root.addclass('fouc')
    }
  }

  componentwillmount () {
    this.togglefoucclass()
  }

  componentdidmount () {
    this.props.load()
      .then((component) => {
        settimeout(() => this.togglefoucclass(), 0)
        this.setstate(() => ({
          component: component.default
        }))
      })
  }

  render () {
    return this.props.children(this.state.component)
  }
}

const asyncroute = (importfunc) =>
  (props) => (
    <asyncimport load={importfunc}>
      {(component) => {
        return component === null
          ? <loader loading />
          : <component {...props} />
      }}
    </asyncimport>
  )

export default asyncroute

hasclass, addclass, removeclass are polyfills which operates on dom class attribute.

loader is a custom component which shows spinner.

why settimeout?

just because we need to remove fouc class in the second tick. otherwise it would happen in the same as rendering the component. so it won't work.

as you can see in the asyncimport component we modify react root container by adding fouc class. so html for clarity:

<html lang="en">
<head></head>
<body>
  <div id="react-app"></div>
</body>
</html>

and another piece of puzzle:

#react-app.fouc
    .page-layout *
        visibility: hidden

sass to apply when importing of specific component (ie.: home, example) takes place.

why not display: none?

because we want to have all components which rely on parent width, height or any other css rule to be properly rendered.

how it works?

the main assumption was to hide all elements until compoment gets ready to show us rendered content. first it fires asyncroute function which shows us loader until component mounts and renders. in the meantime in asyncimport we switch visibility of content by using a class fouc on react root dom element. when everything loads, it's time to show everything up, so we remove that class.

hope that helps!

thanks to

this article, which idea of dynamic import has been taken (i think) from react-loadable.

source

https://turkus.github.io/2018/06/06/fouc-react/


Related Query

More Query from same tag