Layout Persistence in Next.js Ozan Bolel on June 15, 2020 Although the word layout is generally related to CSS page layout, the reason why I’m posting this is a bit different. Fixing the position of navbar... Read full post Submit Preview Dismiss Collapse Expand Léo Stewart Léo Stewart Léo Stewart Follow Joined Nov 28, 2017 • Jun 16 '20 Dropdown menu Copy link Hide Hi, I wonder why do you bother to import MyLayout in every pages when just import MyLayout in the _app work the same, without this MyPage.Layout = MyLayout; ? I hav checkd and id works as expected and in a simplier way. Leo Collapse Expand Ozan Bolel Ozan Bolel Ozan Bolel Follow self-taught frontend developer Education Industrial Engineering Joined Jun 12, 2020 • Jun 16 '20 • Edited on Jun 16 • Edited Dropdown menu Copy link Hide To have multiple layouts within the app. We wouldn't want all pages to share the same layout. Collapse Expand Léo Stewart Léo Stewart Léo Stewart Follow Joined Nov 28, 2017 • Jun 16 '20 • Edited on Jun 16 • Edited Dropdown menu Copy link Hide Oh ! That make sense. Maybe it could be easier with a custom hook then ? Like : import { useState } from "react"; const FirstLayout = ({ children }) => { const [counter, setCounter] = useState(0); return ( <> <p> First Layout <button onClick={() => setCounter(counter + 1)}> Clicked {counter} Times </button> </p> {children} </> ) } export default FirstLayout export function withFirstLayout (Component){ Component.Layout = FirstLayout return Component } Enter fullscreen mode Exit fullscreen mode and the page : import { withFirstLayout } from 'layout/FirstLayout' const Home = () => { return <div> index </div> } export default withFirstLayout(Home) Enter fullscreen mode Exit fullscreen mode But when we change the layout we still have a reset of the old one Collapse Expand Igor Cunha Igor Cunha Igor Cunha Follow Joined Sep 3, 2020 • Sep 3 '20 Dropdown menu Copy link Hide Any idea, how to implement Layout with typescript? Collapse Expand Ozan Bolel Ozan Bolel Ozan Bolel Follow self-taught frontend developer Education Industrial Engineering Joined Jun 12, 2020 • Sep 3 '20 Dropdown menu Copy link Hide import * as React from "react"; import { NextPage } from "next"; type Page = NextPage & { Layout?: React.FC }; Enter fullscreen mode Exit fullscreen mode Collapse Expand Igor Cunha Igor Cunha Igor Cunha Follow Joined Sep 3, 2020 • Sep 3 '20 Dropdown menu Copy link Hide Thanks, I was blowing my mind trying to figure that. Aris Pikeas Aris Pikeas Aris Pikeas Follow Joined Sep 23, 2020 • Sep 23 '20 Dropdown menu Copy link Hide Could you share a bit more context on how you got this working with Typescript?Component.layout is throwing "unsafe assignment of an any value". Ozan Bolel Ozan Bolel Ozan Bolel Follow self-taught frontend developer Education Industrial Engineering Joined Jun 12, 2020 • Sep 26 '20 Dropdown menu Copy link Hide import * as React from "react"; import { NextPage } from "next"; type Page = NextPage & { Layout?: React.FC }; const MyPageComponent: Page = () => { return null; } export default MyPageComponent; Aris Pikeas Aris Pikeas Aris Pikeas Follow Joined Sep 23, 2020 • Sep 26 '20 Dropdown menu Copy link Hide type Page = NextPage & { layout?: (props: { children: ReactNode }) => JSX.Element } Enter fullscreen mode Exit fullscreen mode This seems to have done the trick for me when not using fat-arrow functions. Collapse Expand thomas159 thomas159 thomas159 Follow Joined Nov 3, 2020 • Nov 3 '20 Dropdown menu Copy link Hide how can I update the title in the head using this layout? Collapse Expand Ozan Bolel Ozan Bolel Ozan Bolel Follow self-taught frontend developer Education Industrial Engineering Joined Jun 12, 2020 • Nov 4 '20 • Edited on Nov 4 • Edited Dropdown menu Copy link Hide Don't. Use title inside Head from next/head instead. Collapse Expand Jorge Jorge Jorge Follow Location Heidelberg, Germany Joined Nov 12, 2020 • Nov 12 '20 • Edited on Nov 12 • Edited Dropdown menu Copy link Hide Hi, do you mean to use the Head tag and title tag inside every page? Would it be possible to have the Head tag in the layout component and pass the title variable somehow when you are doing Page.Layout = MyLayout? Ozan Bolel Ozan Bolel Ozan Bolel Follow self-taught frontend developer Education Industrial Engineering Joined Jun 12, 2020 • Nov 12 '20 • Edited on May 29 • Edited Dropdown menu Copy link Hide Yes I meant that. It is possible but it adds an unnecessary complexity imho. Joshua Tuscan Joshua Tuscan Joshua Tuscan Follow Joshua Tuscan heads the technology practice at Applied Poetics and Éditions. Email hello@joshuatuscan.com Location New York Work Partner at Applied Poetics and Éditions Joined Nov 12, 2018 • Jul 16 '21 Dropdown menu Copy link Hide What if you want to pass a prop to the MyLayout so that you can let your nav component know what page you are on? How do you pass props to MyLayout? Collapse Expand artydev artydev artydev Follow Joined Jul 17, 2019 • Jun 9 '24 Dropdown menu Copy link Hide Thank you Collapse Expand MacGyver98 MacGyver98 MacGyver98 Follow Joined Mar 22, 2021 • Mar 22 '21 Dropdown menu Copy link Hide Hi! thanks for your effort and share us. What about if I need to decide between two different layouts? Where should I put the logic and how should I export Layout from my page? Collapse Expand Nerdherd Nerdherd Nerdherd Follow The Good SWE Joined Mar 26, 2019 • May 29 '21 • Edited on May 29 • Edited Dropdown menu Copy link Hide You will assign the layout to the page in the page file. Upon navigating to a page, _app.js will render the layout from the page component via Component.Layout as per the authors code. // pages/home.js import HomeLayout from '../layouts/home-layout.jsx const HomePage = () => ( <div>HomePage</div> ) // assigning HomeLayout to the page property `Layout` HomePage.Layout = HomeLayout export default HomePage Enter fullscreen mode Exit fullscreen mode // pages/account.js import AccountLayout from '../layouts/account-layout.jsx const AccountPage = () => ( <div>Account Page</div> ) AccountPage.Layout = AccountLayout export default AccountPage Enter fullscreen mode Exit fullscreen mode Code of Conduct • Report abuse
Hi,
I wonder why do you bother to import MyLayout in every pages when just import MyLayout in the _app work the same, without this
MyPage.Layout = MyLayout;?I hav checkd and id works as expected and in a simplier way.
Leo
To have multiple layouts within the app. We wouldn't want all pages to share the same layout.
Oh ! That make sense. Maybe it could be easier with a custom hook then ?
Like :
and the page :
But when we change the layout we still have a reset of the old one
Any idea, how to implement Layout with typescript?
Thanks, I was blowing my mind trying to figure that.
Could you share a bit more context on how you got this working with Typescript?Component.layout is throwing "unsafe assignment of an any value".
This seems to have done the trick for me when not using fat-arrow functions.
how can I update the title in the head using this layout?
Don't. Use title inside Head from next/head instead.
Hi, do you mean to use the Head tag and title tag inside every page?
Would it be possible to have the Head tag in the layout component and pass the title variable somehow when you are doing Page.Layout = MyLayout?
Yes I meant that.
It is possible but it adds an unnecessary complexity imho.
What if you want to pass a prop to the MyLayout so that you can let your nav component know what page you are on? How do you pass props to MyLayout?
Thank you
Hi! thanks for your effort and share us.
What about if I need to decide between two different layouts? Where should I put the logic and how should I export Layout from my page?
You will assign the layout to the page in the page file.
Upon navigating to a page,
_app.jswill render the layout from the page component viaComponent.Layoutas per the authors code.