Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Next.js] Remove withSitecoreContext HOC from Layout.tsx #887

Merged
merged 5 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"license": "Apache-2.0",
"dependencies": {
"@sitecore-jss/sitecore-jss-nextjs": "^20.0.0-canary",
"deep-equal": "^2.0.5",
"graphql": "~15.4.0",
"graphql-tag": "^2.11.0",
"next": "^12.0.4",
Expand All @@ -47,7 +46,6 @@
"@graphql-codegen/typescript-resolvers": "^1.17.10",
"@graphql-typed-document-node/core": "^3.1.0",
"@sitecore-jss/sitecore-jss-cli": "^20.0.0-canary",
"@types/deep-equal": "^1.0.1",
"@types/node": "^14.6.4",
"@types/react": "^17.0.15",
"@types/react-dom": "^17.0.9",
Expand Down
22 changes: 9 additions & 13 deletions packages/create-sitecore-jss/src/templates/nextjs/src/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React from 'react';
import Head from 'next/head';
import deepEqual from 'deep-equal';
import {
Placeholder,
VisitorIdentification,
withSitecoreContext,
getPublicUrl,
SitecoreContextValue,
LayoutServiceData,
} from '@sitecore-jss/sitecore-jss-nextjs';
import Navigation from 'src/Navigation';

Expand All @@ -15,10 +13,12 @@ import Navigation from 'src/Navigation';
const publicUrl = getPublicUrl();

interface LayoutProps {
sitecoreContext: SitecoreContextValue;
layoutData: LayoutServiceData;
}

const Layout = ({ sitecoreContext: { route } }: LayoutProps): JSX.Element => {
const Layout = ({ layoutData }: LayoutProps): JSX.Element => {
const { route } = layoutData.sitecore;

return (
<>
<Head>
Expand All @@ -38,16 +38,12 @@ const Layout = ({ sitecoreContext: { route } }: LayoutProps): JSX.Element => {
<Navigation />
{/* root placeholder for the app, which we add components to using route data */}
<div className="container">
{route && <Placeholder name="<%- appPrefix ? `${appName}-` : '' %>jss-main" rendering={route} />}
{route && (
<Placeholder name="<%- appPrefix ? `${appName}-` : '' %>jss-main" rendering={route} />
)}
</div>
</>
);
};

const propsAreEqual = (prevProps: LayoutProps, nextProps: LayoutProps) => {
if (deepEqual(prevProps.sitecoreContext.route, nextProps.sitecoreContext.route)) return true;

return false;
};

export default withSitecoreContext()(React.memo(Layout, propsAreEqual));
export default Layout;
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const SitecorePage = ({ notFound, componentProps, layoutData }: SitecorePageProp
return (
<ComponentPropsContext value={componentProps}>
<SitecoreContext componentFactory={componentFactory} layoutData={layoutData}>
<Layout />
<Layout layoutData={layoutData} />
</SitecoreContext>
</ComponentPropsContext>
);
Expand Down