Code Monkey home page Code Monkey logo

Comments (10)

altaywtf avatar altaywtf commented on April 28, 2024 19

Just an idea: you may consider to write an higher order component that takes your page component and returns some structure like this

<section>
   <Header />
   <YourPageComponent />
   <Footer />
</section>

Update:

components/AppLayout.js

import React, { Component } from 'react';
import { Header, Footer } from 'components';

const withAppLayout = () => (ComposedComponent) => {
  class AppLayout extends Component {
    render() {
      return (
        <section>
           <Header />
           <ComposedComponent {...this.props} />
           <Footer />
         </section>
      );
    }
  }

  return AppLayout;
};

export default withAppLayout;

pages/index.js

import withAppLayout from 'components/AppLayout';

const Home = () => (<div>Home</div>);

export default withAppLayout(Home);

from next.js.

impronunciable avatar impronunciable commented on April 28, 2024 15

@davidrossjones one option is:

// components/Layout.js

export default () => (
  <div>
    <h1>Header</h1>
    {this.props.children}
    <footer>the footer</footer>
  </div>
)
// pages/index.js

import Layout from '../components/Layout'

export default () => (
  <Layout>
    <p>Home</p>
  </Layout>
)
// pages/about.js

import Layout from '../components/Layout'

export default () => (
  <Layout>
    <p>About</p>
  </Layout>
)

from next.js.

dlindenkreuz avatar dlindenkreuz commented on April 28, 2024 6

@impronunciable @altayaydemir Apparently, the Layout wrapper component gets re-mounted when changing routes which does not completely solve the problem. @ericf describes this in #50.

from next.js.

cupliz avatar cupliz commented on April 28, 2024 3

how to get initial props using this layout? like this

static getInitialProps ({ req}) {
  return {request: req ? 'Server' : 'Client '}
}

from next.js.

davidrossjones avatar davidrossjones commented on April 28, 2024 1

Aha! That makes perfect sense.

Thank you @altayaydemir and @impronunciable for the insight! Awesome. πŸ‘

from next.js.

altaywtf avatar altaywtf commented on April 28, 2024 1

@dlindenkreuz well, you are right. πŸ˜₯ I also agree on having an app layout component.

from next.js.

davidrossjones avatar davidrossjones commented on April 28, 2024

Thank you for your reply πŸ˜ƒ .

Where would I specify this HOC in the context of next.js though?

The default router will render index.js for / and about.js for /about. Do I have to duplicate for both?

index.js

export default () => (
  <div><Header/>Home<Footer/></div>
)

about.js

export default () => (
  <div><Header/>About<Footer/></div>
)

from next.js.

mpint avatar mpint commented on April 28, 2024

In the React context, when building an app I'm used to the concept of containers and components. It appears to me that "pages" are somewhat equivalent to containers within next.js.

I was wondering the same thing as well. Is there anything fundamentally different between pages and containers?

from next.js.

Katyadelarosa avatar Katyadelarosa commented on April 28, 2024

Wondering the same over here.

I was used more to.

./pages/Index.js

const IndexPage = ({ serverCall }) => (
 <div>
   <button onClick={serverCall}>Hello</button>
</div>
);

export default IndexPage;

And then on.

 ./containers/Index.is

import IndexPage from './pages/Index.js'

const composerData = (props, onData) => {
   onData(null, {
     serverCall: () => console.log('call');
   })
};
export default compose(composerData)(IndexPage);

where compose can be something like https://github.com/arunoda/react-komposer, but still i'm not sure if this the correct usage for NextJs, or i can avoid the Container and do this on the Page directly

from next.js.

timneutkens avatar timneutkens commented on April 28, 2024

You could use pages/_app.js for global layout now.

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.