Skip to content

Commit

Permalink
Added lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoTomeES committed Dec 18, 2019
1 parent 045db29 commit 1ea39e9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ import {connect} from 'react-redux';

import Layout from './hoc/Layout/Layout';
import BurgerBuilder from './containers/BurgerBuilder/BurgerBuilder';
import Checkout from './containers/Checkout/Checkout';
import Orders from './containers/Orders/Orders';
import Auth from './containers/Auth/Auth';
import Logout from './containers/Auth/Logout/Logout';
import * as actions from './store/actions/auth';
import asyncComponent from './hoc/asyncComponent/asyncComponent';

const asyncCheckout = asyncComponent(() => {
return import('./containers/Checkout/Checkout');
});

const asyncOrders = asyncComponent(() => {
return import('./containers/Orders/Orders');
});

const asyncAuth = asyncComponent(() => {
return import('./containers/Auth/Auth');
});

class App extends Component {
componentDidMount() {
Expand All @@ -18,7 +28,7 @@ class App extends Component {
render() {
let routes = (
<Switch>
<Route path="/auth" component={Auth} />
<Route path="/auth" component={asyncAuth} />
<Route path="/" exact component={BurgerBuilder} />
<Redirect to="/" />
</Switch>
Expand All @@ -27,10 +37,10 @@ class App extends Component {
if (this.props.isAuthenticated) {
routes = (
<Switch>
<Route path="/checkout" component={Checkout} />
<Route path="/orders" component={Orders} />
<Route path="/checkout" component={asyncCheckout} />
<Route path="/orders" component={asyncOrders} />
<Route path="/logout" component={Logout} />
<Route path="/auth" component={Auth} />
<Route path="/auth" component={asyncAuth} />
<Route path="/" exact component={BurgerBuilder} />
<Redirect to="/" />
</Switch>
Expand Down
24 changes: 24 additions & 0 deletions src/hoc/asyncComponent/asyncComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { Component } from 'react';

const asyncComponent = (importComponent) => {
return class extends Component {
state = {
component: null
}

componentDidMount () {
importComponent()
.then(cmp => {
this.setState({component: cmp.default});
});
}

render () {
const C = this.state.component;

return C ? <C {...this.props} /> : null;
}
}
}

export default asyncComponent;
1 change: 0 additions & 1 deletion src/store/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const authSuccess = (token, userId) => {
};

export const authFail = (error) => {
console.log(error)
return {
type: actionTypes.AUTH_FAIL,
error: error
Expand Down

0 comments on commit 1ea39e9

Please sign in to comment.