Skip to content

Commit f6ae27f

Browse files
committed
Layout rerender fix
1 parent 20c08ec commit f6ae27f

File tree

12 files changed

+85
-28
lines changed

12 files changed

+85
-28
lines changed

docs/references.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ https://www.robinwieruch.de/react-state-usereducer-usestate-usecontext/
1414
React with Redux and hooks
1515
https://medium.com/@koss_lebedev/rematch-with-hooks-50a8380c46e4
1616

17+
Layouts
18+
https://stackoverflow.com/questions/33996484/using-multiple-layouts-for-react-router-components
19+
https://gist.github.com/avinmathew/e82fe7e757b20cb337d5219e0ab8dc2c
20+
1721
#### TypeScript
1822

1923
Typescript Book:

src/AppRoute.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react'
2+
import { Route } from 'react-router-dom'
3+
4+
const AppRoute = ({ component: Component, layout: Layout, ...rest }: any) => (
5+
<Route
6+
{...rest}
7+
render={props => {
8+
if (Layout) {
9+
return (
10+
<Layout>
11+
<Component {...props} />
12+
</Layout>
13+
)
14+
} else {
15+
return <Component {...props} />
16+
}
17+
}}
18+
/>
19+
)
20+
21+
export default AppRoute

src/AppRouter.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import config from './_config'
1313
import authService from './_services/authService'
1414
import { useAppState, useAppStateMethods } from './_state/appState'
1515

16+
import AppRoute from './AppRoute'
17+
import DashboardLayout from '_layouts/DashboardLayout'
18+
1619
// Import application modules
1720
import Sales from './SalesModule'
1821
import Content from './ContentModule'
@@ -41,13 +44,13 @@ const LoggedInRouter = () => {
4144
return (
4245
<Switch>
4346
<Route exact path="/" render={() => <Redirect to="/sales/dashboard" />} />
44-
<Route path={`/sales`} component={Sales} />
45-
<Route path={`/content`} component={Content} />
46-
<Route path={`/admin`} component={Admin} />
47-
<Route path={`/profile`} component={Profile} />
48-
<Route path={`/account`} component={Organization} />
49-
<Route path={`/demo`} component={Demo} />
50-
<Route component={NotFound} />
47+
<AppRoute path={`/sales`} component={Sales} layout={DashboardLayout} />
48+
<AppRoute path={`/content`} component={Content} layout={DashboardLayout} />
49+
<AppRoute path={`/admin`} component={Admin} layout={DashboardLayout} />
50+
<AppRoute path={`/profile`} component={Profile} layout={DashboardLayout} />
51+
<AppRoute path={`/account`} component={Organization} layout={DashboardLayout} />
52+
<AppRoute path={`/demo`} component={Demo} layout={DashboardLayout} />
53+
<AppRoute component={NotFound} layout={DashboardLayout} />
5154
</Switch>
5255
)
5356
}
@@ -58,8 +61,10 @@ const AppRouterComponent: React.ComponentType<any> =
5861

5962
const AppRouter: React.FC = () => (
6063
<AppRouterComponent>
61-
<Route path="/auth" component={Auth} />
62-
<PrivateRoute path="/" component={LoggedInRouter} />
64+
<Switch>
65+
<Route path="/auth" component={Auth} />
66+
<PrivateRoute path="/" component={LoggedInRouter} />
67+
</Switch>
6368
</AppRouterComponent>
6469
)
6570

src/DemoModule/Demo.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import { Switch, Route, RouteComponentProps } from 'react-router-dom'
33

4+
import AppRoute from 'AppRoute'
45
import DashboardLayout from '_layouts/DashboardLayout'
56

67
import Features from './Features'
@@ -12,14 +13,12 @@ export interface DemoProps extends RouteComponentProps {}
1213

1314
const Demo: React.FC<DemoProps> = ({ match }) => {
1415
return (
15-
<DashboardLayout>
16-
<Switch>
17-
<Route path={`${match.url}/features`} component={Features} />
18-
<Route path={`${match.url}/docs`} component={Docs} />
19-
<Route path={`${match.url}/supporters`} component={Supporters} />
20-
<Route path={`${match.url}/discuss`} component={Discuss} />
21-
</Switch>
22-
</DashboardLayout>
16+
<Switch>
17+
<AppRoute path={`${match.url}/features`} component={Features} />
18+
<AppRoute path={`${match.url}/docs`} component={Docs} />
19+
<AppRoute path={`${match.url}/supporters`} component={Supporters} />
20+
<AppRoute path={`${match.url}/discuss`} component={Discuss} />
21+
</Switch>
2322
)
2423
}
2524

src/EmptyModule/_api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {}

src/EmptyModule/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {}

src/SalesModule/Customers/Customers.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import React from 'react'
22
// import clsx from 'clsx'
33
import { makeStyles } from '@material-ui/core/styles'
44

5+
// import DashboardLayout from '_layouts/DashboardLayout'
6+
// import { withDashboardLayout } from '_layouts'
57
import PageContainer from '../../_common/PageContainer'
6-
7-
// import Grid from '@material-ui/core/Grid'
88
import Paper from '@material-ui/core/Paper'
99

1010
import { Theme } from '_theme'
1111

12-
export default function Customers() {
12+
const Customers = () => {
1313
const classes = useStyles()
1414

1515
return (
@@ -20,3 +20,5 @@ export default function Customers() {
2020
}
2121

2222
const useStyles = makeStyles((theme: Theme) => ({}))
23+
24+
export default Customers

src/SalesModule/Orders/Orders.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Button from '@material-ui/core/Button'
66

77
// import { Theme } from '_theme'
88

9+
// import DashboardLayout from '_layouts/DashboardLayout'
10+
// import { withDashboardLayout } from '_layouts'
911
import PageContainer from '../../_common/PageContainer'
1012
import PageToolbar from '../../_common/PageToolbar'
1113

src/SalesModule/Overview/Overview.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import moment from 'moment'
55

66
import Grid from '@material-ui/core/Grid'
77

8+
// import { withDashboardLayout } from '_layouts'
89
import PageContainer from '../../_common/PageContainer'
910
import PageToolbar from '../../_common/PageToolbar'
1011

@@ -14,7 +15,7 @@ import SalesDashboardActions from './OverviewActions'
1415
import OrdersHistory from './OrdersHistory'
1516
import OrdersLatest from './OrdersLatest'
1617

17-
export default function SalesDashboard() {
18+
const SalesDashboard = () => {
1819
const [filter, setFilter] = useState({
1920
dateFrom: moment()
2021
.subtract(14, 'day')
@@ -59,3 +60,5 @@ export default function SalesDashboard() {
5960
// paddingBottom: theme.spacing(4),
6061
// },
6162
// }))
63+
64+
export default SalesDashboard

src/SalesModule/Sales.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import { Switch, Route, RouteComponentProps } from 'react-router-dom'
33

4+
import AppRoute from 'AppRoute'
45
import DashboardLayout from '_layouts/DashboardLayout'
56
import Overview from './Overview'
67
import Orders from './Orders'
@@ -10,13 +11,15 @@ export interface SalesProps extends RouteComponentProps {}
1011

1112
const Sales = ({ match }: SalesProps) => {
1213
return (
13-
<DashboardLayout>
14-
<Switch>
15-
<Route path={`${match.url}/dashboard`} component={Overview} />
16-
<Route path={`${match.url}/orders`} component={Orders} />
17-
<Route path={`${match.url}/customers`} component={Customers} />
18-
</Switch>
19-
</DashboardLayout>
14+
<Switch>
15+
{/* <Route path={`${match.url}/dashboard`} component={Overview} /> */}
16+
{/* <Route path={`${match.url}/orders`} component={Orders} /> */}
17+
{/* <Route path={`${match.url}/customers`} component={Customers} /> */}
18+
19+
<AppRoute path={`${match.url}/dashboard`} component={Overview} />
20+
<AppRoute path={`${match.url}/orders`} component={Orders} />
21+
<AppRoute path={`${match.url}/customers`} component={Customers} />
22+
</Switch>
2023
)
2124
}
2225

0 commit comments

Comments
 (0)