Closed morellodev closed 2 years ago
Hi @morellodev, this happens because gatsby-browser.js
/gatsby-ssr.js
are using require()
while your pages are likely using import
.
Using require()
will point at @prismicio/react
's CommonJS file, while import
will use its ESM file. Each file has its own React context object. usePrismicContext()
from the ESM file cannot "see" the PrismicProvider
from the CommonJS file.
You can fix this by writing your gatsby-browser.js
/gatsby-ssr.js
using ESM syntax:
// gatsby-ssr.js and gatsby-browser.js
import * as React from 'react'
import { Link } from 'gatsby'
import { PrismicProvider } from '@prismicio/react'
import linkResolver from './linkResolver'
export const wrapRootElement = ({ element }) => {
return (
<PrismicProvider
linkResolver={linkResolver}
internalLinkComponent={({ href, children, ...otherProps }) => (
<Link to={href} {...otherProps}>
{children}
</Link>
)}
externalLinkComponent={({ href, target, children, ...otherProps }) => {
const targetProps = target ? { target, rel: 'noopener' } : {}
return (
<a href={href} {...otherProps} {...targetProps}>
{children}
</a>
)
}}
>
{element}
</PrismicProvider>
)
}
If this doesn't fix your issue, let me know and I'll reopen the issue. Thanks! 🙂
Thank you @angeloashmore, I will try your fix and eventually I will come back here again 😃
It worked! Thanks a lot 👍
Glad to hear it's working! 😀
Versions
Description
I am adding a
linkResolver
function and a custominternalLinkComponent
to the entire Gatsby website, putting the<PrismicProvider>
inside thewrapRootElement
(both ingatsby-ssr.js
and ingatsby-browser.js
). If I open the React devtools, I can see thePrismicProvider
in the components tree, but every child that access the value exposed by it receives an empty object.React devtools (
<PrismicProvider>
):React devtools (child accessing the
PrismicContext
):