Comments (10)
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.
@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.
@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.
how to get initial props using this layout? like this
static getInitialProps ({ req}) {
return {request: req ? 'Server' : 'Client '}
}
from next.js.
Aha! That makes perfect sense.
Thank you @altayaydemir and @impronunciable for the insight! Awesome. π
from next.js.
@dlindenkreuz well, you are right. π₯ I also agree on having an app layout component.
from next.js.
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.
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.
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.
You could use pages/_app.js
for global layout now.
from next.js.
Related Issues (20)
- Relative <img> 404s when using next/link and trailingSlash HOT 2
- Triggering notFound in generateMetadata results in Unhandled Runtime Error Error: NEXT_NOT_FOUND
- File name too long (os error 63) since > 14.2 HOT 2
- Docs: Pages and layouts section not opening HOT 5
- Page won't rerender when initial searchparam is removed HOT 1
- Typescript type for router.query is incorrect since it can be undefined (on first render in frontend) HOT 1
- Typescript type for router.query is incorrect since it can be undefined (on first render in frontend) HOT 1
- Routes are broken when the page extension contains only 'tsx'. HOT 2
- Docs: Allow https://nextjs.org/ to work when PerformanceObserver is undefined
- v14.2.x gives warning for `Invalid page configuration` for `middleware.ts` HOT 2
- Dependencies with `exports` field break on pages router with `esmExternals: false`
- Intercepted page in dynamic route with route groups gets called when linked from another segment
- Rewritten paths cause a client side redirect unexpectedly when set up with basePath: false and an active basePath
- SWC does not catch nested `styled-jsx` tags in props HOT 1
- SWC panicks when processing nested `styled-jsx` tags
- Code with a worker fails to build under the edge runtime, even if it's dead code.
- Runtime error in my local machine in rendering current time HOT 4
- Cdn problem with App Router HOT 1
- Values ββsent by headers in the middleware are not accepting letters with accents HOT 1
- Docker image ignores signals
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next.js.