0

In JSS react how to access the layout service response object sitecore.context in src/Layout.js.

If you look at src/Layout.js file, In default we can access only sitecore.route fields but not sitecore.context.

enter image description here

2 Answers 2

1

We can achieve this in one way.

Go to \src\RouteHandler.js here sitecore.context will be available.

Change the line like below, (This will pass context object to layout)

// Original line
return <Layout route={layoutData.route} />;
// Change this line
return <Layout route={layoutData.route} sitecoreContext={this.props.sitecoreContext} />;

Goto \src\Layout.js, change the line below

// Original line
const Layout = ({ route }) => {
// Change this line
const Layout = ({ sitecoreContext, route }) => {

We can access the sitecoreContext object now.

1

We can get Sitecore context using by importing sitecore-jss-react on the component

import { withSitecoreContext } from '@sitecore-jss/sitecore-jss-react';

and use this service to access context item as follows:

const MyComponent = (props) => <div>Page editing: {props.sitecoreContext.pageEditing}</div>;
// wrap MyComponent in the HOC (note the double function invocation - important)
export default withSitecoreContext()(MyComponent);

You can get more detail from here

I hope it helps you.

1
  • 1
    The question is accessed in layout.js file, not in the component. Above snippet not working at layout.js
    – Andi AR
    Commented May 30, 2023 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.