react router lazy component

Adam

So this works:

import Page from 'components/Page';
...

render() {
   return (
     <Route render={props => <Page {...props}/>}/>
   );
}

But this doesn't:

import React, { lazy } from 'react';
const Cmp = lazy(() => import('components/Page'));
...

render() {
   return (
     <Route render={props => <Cmp {...props}/>}/>
   );
}

React 16.8.6 React Router 5.0.0

I get this:

The above error occurred in one of your React components:
    in Unknown (at configured.route.js:41)
    in Route (at configured.route.js:41)
    in ConfiguredRoute (created by Context.Consumer)
    ...rest of stack trace

Can anyone see what stupid thing I am doing here?

John Ruddell

Referencing the React Docs on code splitting, the recommendation is to use Suspense with a defined fallback so you have something to render in place of the components when they haven't loaded.

// Direct paste from https://reactjs.org/docs/code-splitting.html

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import React, { Suspense, lazy } from 'react';

const Home = lazy(() => import('./routes/Home'));
const About = lazy(() => import('./routes/About'));

const App = () => (
  <Router>
    <Suspense fallback={<div>Loading...</div>}>
      <Switch>
        <Route exact path="/" component={Home}/>
        <Route path="/about" component={About}/>
      </Switch>
    </Suspense>
  </Router>
);

Suspense should be a parent of the <Route> elements that use lazy

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

React Router not rendering component initially

分類Dev

Vue-router cannot lazy load child component

分類Dev

React Component not rendering on using react router 4

分類Dev

react-router mounts the wrong component

分類Dev

Problem rendering Component in a different page with React Router

分類Dev

React router renders component after a refresh

分類Dev

ReactJS - React Router not changing component but url is changing

分類Dev

React Router Dom Link in another component

分類Dev

Nested component in react-router is not rendering

分類Dev

How to call a component inside a component using React Router

分類Dev

React Router - Use component parameter and pass in props to rendered component?

分類Dev

React router dom passing data from parent component to child router component does not pass the props.match

分類Dev

React-router doesn't render component after adding history

分類Dev

React Router 4 can not load new content on same component with <Link>

分類Dev

React-Router - Route re-rendering component on route change

分類Dev

Why is state undefined when passed through React Router Link Component?

分類Dev

react router dom is not updating class component when url change

分類Dev

How can I pass the component's name from another component's object to Route in React Router Dom?

分類Dev

How to lazy load component and template

分類Dev

React.lazy() with Typescript

分類Dev

Router Resolver not rendering component

分類Dev

Vue Random Router Component

分類Dev

How do I transmit a state to a parent component when clicking a react-router Link?

分類Dev

Semantic-UI Sidebar.Pusher causing react router to rerender its component

分類Dev

React Router Switch statement with routes grouped as component inside does not go to Not Found route

分類Dev

Prevent nested <Link/> component from inheriting path from previous <Link/> (React router 4)

分類Dev

React-Router: How do I add a new component and route to the onboarding steps on a wizard?

分類Dev

How to pass a prop from one component to another when using react router link

分類Dev

React-router-dom how to update parent state inside route component

Related 関連記事

  1. 1

    React Router not rendering component initially

  2. 2

    Vue-router cannot lazy load child component

  3. 3

    React Component not rendering on using react router 4

  4. 4

    react-router mounts the wrong component

  5. 5

    Problem rendering Component in a different page with React Router

  6. 6

    React router renders component after a refresh

  7. 7

    ReactJS - React Router not changing component but url is changing

  8. 8

    React Router Dom Link in another component

  9. 9

    Nested component in react-router is not rendering

  10. 10

    How to call a component inside a component using React Router

  11. 11

    React Router - Use component parameter and pass in props to rendered component?

  12. 12

    React router dom passing data from parent component to child router component does not pass the props.match

  13. 13

    React-router doesn't render component after adding history

  14. 14

    React Router 4 can not load new content on same component with <Link>

  15. 15

    React-Router - Route re-rendering component on route change

  16. 16

    Why is state undefined when passed through React Router Link Component?

  17. 17

    react router dom is not updating class component when url change

  18. 18

    How can I pass the component's name from another component's object to Route in React Router Dom?

  19. 19

    How to lazy load component and template

  20. 20

    React.lazy() with Typescript

  21. 21

    Router Resolver not rendering component

  22. 22

    Vue Random Router Component

  23. 23

    How do I transmit a state to a parent component when clicking a react-router Link?

  24. 24

    Semantic-UI Sidebar.Pusher causing react router to rerender its component

  25. 25

    React Router Switch statement with routes grouped as component inside does not go to Not Found route

  26. 26

    Prevent nested <Link/> component from inheriting path from previous <Link/> (React router 4)

  27. 27

    React-Router: How do I add a new component and route to the onboarding steps on a wizard?

  28. 28

    How to pass a prop from one component to another when using react router link

  29. 29

    React-router-dom how to update parent state inside route component

ホットタグ

アーカイブ