@mgsong/react-async-component

0.0.3 • Public • Published

react-asynccomponent

Load react component dynamically


Usage

import React, { useEffect } from 'react'
import { BrowserRouter, Route } from 'react-router-dom'
import Loading from './components/Loading'
import AsyncComponent from '@mgsong/react-async-component'

export default function Routes() {
  return (
    <BrowserRouter>
      <Route
        path="/"
        render={({ history, location }) => {
          console.info('[Dynamic Routing] ' + location.pathname)
          return (
            <AsyncComponent
              module={import('./pages' + location.pathname).then(
                (module) => module.default,
              )}
              loading={<Loading />}
              onNotFound={() => {
                history.push('/404')
              }}
            />
          )
        }}
      />
    </BrowserRouter>
  )
}

Package Sidebar

Install

npm i @mgsong/react-async-component

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

2.27 kB

Total Files

3

Last publish

Collaborators

  • keating1729