Last active
January 16, 2022 07:02
-
-
Save DSchau/62b599456047c4ed4dbd5dec56d1a467 to your computer and use it in GitHub Desktop.
Higher Order Components + Gatsby Sitting In a Tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react" | |
import { Link, graphql } from "gatsby" | |
import Layout from "../components/layout" | |
import Image from "../components/image" | |
import SEO from "../components/seo" | |
const withAuthentication = Page => { | |
return class extends React.Component { | |
state = { | |
auth: { | |
authenticated: true | |
} | |
} | |
render() { | |
return ( | |
<Page {...this.state} {...this.props} /> | |
) | |
} | |
} | |
} | |
const IndexPage = ({ data }) => ( | |
<Layout> | |
<SEO title="Home" /> | |
<h1>Hi {data.site.siteMetadata.title}</h1> | |
<p>Welcome to your new Gatsby site.</p> | |
<p>Now go build something great.</p> | |
<div style={{ maxWidth: `300px`, marginBottom: `1.45rem` }}> | |
<Image /> | |
</div> | |
<Link to="/page-2/">Go to page 2</Link> | |
</Layout> | |
) | |
export const query = graphql` | |
{ | |
site { | |
siteMetadata { | |
title | |
} | |
} | |
} | |
` | |
export default withAuthentication(IndexPage) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Brother!
I am trying to implement HOC in gatsby too... to do this I have created a JS file like with-----.js. Then, I have coded as you did in withAuthentication. From my other component, I have exported as default with-----.js passing that particular component as a parameter...This was well in my NEXT JS project when I wanted to make a Private route...and I was successful... but right now in gatsby, same procedure I have followed and failed with the error of
_```
Cannot read properties of undefined (reading 'prototype')
./node_modules/@babel/runtime/helpers/esm/inheritsLoose.js:3
Open in Editor
1 | import setPrototypeOf from "./setPrototypeOf.js";
2 | export default function _inheritsLoose(subClass, superClass) {