JavaScript
x
24
24
1
// project/src/App.js
2
import React, { Suspense, lazy } from "react";
3
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
4
import {NavBar, Loading} from "./components";
5
import './index.css';
6
7
const Home = lazy(() => import('./components/home'));
8
9
function App() {
10
return (
11
<Router>
12
<NavBar/>
13
<Suspense fallback={<Loading/>}>
14
<Switch>
15
<Route exact path="/" component={Home}/>
16
17
</Switch>
18
</Suspense>
19
</Router>
20
);
21
}
22
23
export default App;
24
Other file.
JavaScript
1
11
11
1
// project/src/components/Home.js
2
import React from "react";
3
4
const Home = () => (
5
<div className="home">
6
7
</div>
8
);
9
10
export default Home;
11
The code works but () => import('./components/home')
generates this warning:
Argument type function(): Promise<{readonly default?: function(): any}> is not assignable to parameter type () => Promise<{default: ComponentType}> Type Promise<{readonly default?: function(): any}> is not assignable to type Promise<{default: ComponentType}>
I have already read the other topics and none of them work. Thank you.
Advertisement
Answer
Although I don’t like to complicate things, to remove that warning you have to use this syntax:
JavaScript
1
2
1
const Home = lazy(() => import('./components/Home').then(({default: Home}) => ({default: Home})));
2