Universal React Redux Registration
  • Getting Started
  • Backend Project architecture
  • Frontend Project architecture
  • Clone the repository
  • Set-up the Backend
  • Set-up the Frontend
  • JSON Web Tokens
  • Server & Client Bundles
  • Unit tests
  • Create new pages
  • Restrict Access (Authenticated Users Only)
  • Redux-Form
  • Custom Layouts
  • Custom Page titles and SEO
  • SCSS
  • Fonts & Font Icons
  • Images
Powered by GitBook
On this page

Custom Layouts

You can create a custom layout for any page/route.

Create a new layout file within ‘Frontend/src/client/layouts’

Please use the template below to build your new layout. The actions being dispatched are required to ensure that each component is supplied with the users authStatus. The 'RoutesHelper' component is used to render the routes within the layout component. Feel free to build your layout around this component.

import React, {Component} from 'react';
import RoutesHelper from './../routes/routesHelper';
import classNames from 'classnames';
import {connect} from 'react-redux';
import {withRouter} from 'react-router-dom';
import {getAuthStatus, checkAuthStatus, force_noAuthStatus} from './../actions';
import * as Cookies from 'es-cookie';

class NewLayout extends Component {

    constructor(props){
        super(props);
    }

    componentDidMount(){
        let token = Cookies.get('token');
        if(token){
            this.props.checkAuthStatus(token)
        }else{
            this.props.force_noAuthStatus();
        }
    }

    render() {
        return (
            <div>
                <RoutesHelper {...this.props} authed={this.props.authStatus} authPath='/login' />
            </div>
        );  
    }

};

const mapStateToProps = (state) => ({
    authStatus: state.authStatus.status
});

export default {
    component: connect(mapStateToProps, {getAuthStatus, checkAuthStatus, force_noAuthStatus})(withRouter(NewLayout)),
    loadData: ({dispatch}) => dispatch(getAuthStatus())
};

Now you have a new layout component, lets update our routes.

Open ‘Frontend/src/client/routes/index.js’ and import your new layout

import NewLayout from './path/to/layout';

Find and update your selected page/component within the routes array

{
    path: '/some-page',
    ...NewLayout, <<-- Add your layout component here
    routes: [
        {
            ...SomePageComponent
        }
    ]
},
PreviousRedux-FormNextCustom Page titles and SEO

Last updated 6 years ago