Is there a way to persist a layout for a given set of routes?
September 26, 2018 at 11:04amThe Next.js community has a new home. This thread is preserved for historical purposes. The content of this conversation may be innaccurrate or out of date. Go to new community home →
Is there a way to persist a layout for a given set of routes?
September 26, 2018 at 11:04amI see that you can modify _app.js to persist a layout between pages. But lets say I have a /home, /about, and /login page that I don't want a persisted layout. But then I have /dashboard /dashboard/clients /dashboard/statistics where I want a dashboard layout to persist. Is there a way to achieve this? Thanks :)
September 27, 2018 at 11:50pm
September 28, 2018 at 11:40am
Oh I guess it's as simple as passing a function from _app.js down haha. Sometimes when trying something new things seem like magic. Glad next is so simple. Thanks for this idea!
_error.js:
import React from 'react'export default class Error extends React.Component {static getInitialProps ({ res, err }) {const statusCode = res ? res.statusCode : err ? err.statusCode : nullreturn { statusCode }}static isErrorPage = truerender () {return (<h1>{this.props.statusCode? `An error ${this.props.statusCode} occurred on server`: 'An error occurred on client'}</h1>)}}
_app.js:
import App, { Container } from 'next/app'import Layout from 'components/Layout'export default class MyApp extends App {static async getInitialProps ({ Component, router, ctx }) {let pageProps = {}if (Component.getInitialProps) {pageProps = await Component.getInitialProps(ctx)}return { pageProps }}render () {const { Component, pageProps } = this.propsreturn (Component.isErrorPage? (<Container><Component {...pageProps} /></Container>): (<Container><Layout><Component {...pageProps} /></Layout></Container>))}}
This renders the Layout component everywhere except the custom error page.
September 29, 2018 at 11:52am
Also I wanted to clear up that it's probably better to do
static Layout = YourComponent
instead of passing up a true/false value, as that wouldn't scale if you have multiple layouts, or multiple conditions Say if you have 2 or 3 layouts they'd all be shipped in
_app.js
to the client side, whereas when using static Layout = YourComponent
would only have the specific layout needed to render the pageUsing
static Layout
is also more flexible as the page defines what layout to render and it can be wrapped etcAlternatively you could use
next/dynamic
to only load the javascript that has been rendered in _app.jsOctober 8, 2018 at 6:31pm
This would probably make a great first contribution, i.e. https://github.com/zeit/next.js/tree/master/examples
October 9, 2018 at 3:20am
October 10, 2018 at 3:59pm
In _app.js
<Container><Component.Layout><Component { ...pageProps} /></Component.Layout></Container>
In pages/mypage.js
const MyPage = () => <div></div>MyPage.Layout = MyLayout;MyPage.getInitialProps = async () => ({});export default MyPage;
August 15, 2019 at 10:20am
August 21, 2019 at 2:43pm
So this works.. except now I have a server/client mismatch with Styled components.
could this be a timing issue?
November 6, 2019 at 10:39am
The above solutions work, how could i do something similar to what proposed but for .mdx files?
Using a static true/false works, however is it possible to implement something like Page.Layout = Layout for .mdx files?