Skip to content

Commit 5a57bf5

Browse files
committed
update error handler
1 parent cc51a49 commit 5a57bf5

File tree

3 files changed

+56
-26
lines changed

3 files changed

+56
-26
lines changed

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function App() {
3636
}}
3737
onError={(e, history) => {
3838
if (
39-
e.message.startsWith('Cannot find module') &&
39+
/not find module/.test(e.message) &&
4040
window.location.pathname !== '/404'
4141
) {
4242
history.push('/404')
@@ -74,7 +74,50 @@ Then, routed like below automatically
7474

7575
<br>
7676

77-
### Recipe of dynamic path
77+
### Recipe
78+
79+
#### Handling `not find module` error
80+
81+
load 404 page keeping URL
82+
83+
```js
84+
import React from 'react'
85+
import { BrowserRouter } from 'react-router-dom'
86+
import DynamicRoute from 'react-dynamic-route'
87+
88+
export default function App() {
89+
return (
90+
<BrowserRouter>
91+
<DynamicRoute
92+
page={path =>
93+
import('./pages' + path)
94+
.then(module => module.default)
95+
.catch(e => {
96+
if (/not find module/.test(e.message)) {
97+
return import('./pages/404').then(module => module.default)
98+
}
99+
throw e
100+
})
101+
}
102+
loading={<div>Loading..</div>}
103+
props={{
104+
someProp1,
105+
someProp2, // `someProp1` and `someProp2` are transfered to `module.dedault` above finally
106+
}}
107+
onError={(e, history) => {
108+
if (/Loading chunk \d+ failed/.test(e.message)) {
109+
window.location.reload()
110+
return
111+
}
112+
throw e
113+
}}
114+
/>
115+
</BrowserRouter>
116+
)
117+
}
118+
```
119+
120+
#### dynamic path
78121

79122
You can also use dynamic path like below
80123

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-dynamic-route",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Load react component dynamically based on url path",
55
"keywords": [
66
"react",

src/index.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,16 @@ export default function DynamicRoute(props) {
3838
return (
3939
<Route
4040
path="/"
41-
render={({ history }) => {
42-
const onError = e => {
43-
if (
44-
e.message.startsWith('Cannot find module') &&
45-
window.location.pathname !== '/404'
46-
) {
47-
history.push('/404')
48-
return
49-
}
50-
throw e
51-
}
52-
53-
return (
54-
<AsyncComponent
55-
path={window.location.pathname}
56-
component={props.page(window.location.pathname)}
57-
loading={props.loading || 'Loading..'}
58-
onError={props.onError || onError}
59-
otherProps={props.props}
60-
history={history}
61-
/>
62-
)
63-
}}
41+
render={({ history }) => (
42+
<AsyncComponent
43+
path={window.location.pathname}
44+
component={props.page(window.location.pathname)}
45+
loading={props.loading || 'Loading..'}
46+
onError={props.onError}
47+
otherProps={props.props}
48+
history={history}
49+
/>
50+
)}
6451
/>
6552
)
6653
}

0 commit comments

Comments
 (0)