Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to create a page template in React?

Basically I want to include a header, sidebar and footer to each page. I've currently got the above mentioned on each individual page with react router clicking through to each of them. I want to down size my code and have one main template that allows each main section of each page to be unique with the header, footer and sidebar nav in place. What's the best place to add this? Tried in the App.js and index, but doesn't seem to like that.

I'm using antd as my main framework.

Thanks in advance!

 ReactDOM.render((
   <div>
     <Layout>
       <Sider>
         <SideMenu />
       </Sider>
       <Layout>
         <Header />
         <Content style={{ margin: '0 16px' }}>
           <div className='appWrap'>
             <BrowserRouter>
               <LocaleProvider locale={enUS}>
                 <App />
               </LocaleProvider>
             </BrowserRouter>
             <Footer />
           </div>
         </Content>
       </Layout>
     </Layout>
   </div>
), document.getElementById('root'));

Something along the lines of this. I want the template to load around the main App.js I've seen using router to create separate templates won't save me on code as it's what I pretty much have already.

like image 692
Sadie Avatar asked Oct 10 '17 09:10

Sadie


People also ask

Can we use templates in React?

Custom Templates enable you to select a template to create your project from, while still retaining all of the features of Create React App. You'll notice that Custom Templates are always named in the format cra-template-[template-name] , however you only need to provide the [template-name] to the creation command.

Does React use HTML templates?

React doesn't use templates. Traditionally, web application UIs are built using templates or HTML directives. These templates dictate the full set of abstractions that you are allowed to use to build your UI. React approaches building user interfaces differently by breaking them into components.


1 Answers

I'm currently using a similar approach by creating a custom component that passes the props to the Route component:

routes.jsx:

import React, { Fragment } from 'react';
import {
  BrowserRouter as Router,
  Switch
} from 'react-router-dom';
import LayoutDefault from './views/layouts/LayoutDefault';
import Startpage from './views/Startpage';

const Routes = () => (
  <Router>

    <Switch>
      <LayoutDefault exact path="/dashboard" component={Startpage} />
      // ... more routes
    </Switch>

  </Router>
);

export default Routes;

And LayoutDefault.jsx

import React, { Fragment } from 'react';
import { Route } from 'react-router-dom';
import LoggedinMenu from 'modules/Menu/LoggedinMenu';

const LayoutDefault = (props) => (
  <Fragment>
    <LoggedinMenu />
    <Route {...props} />
  </Fragment>
);

export default LayoutDefault;
like image 66
Jonas Sandstedt Avatar answered Sep 21 '22 01:09

Jonas Sandstedt