question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[GatsbyJS] This component must be used inside a <RecoilRoot> component error in build

See original GitHub issue

I’m trying to use Recoil Js in a GatsbyJs application.

I configured <RecoilRoot> in wrapRootElement (also tried import { RecoilRoot } from 'recoil/dist/recoil.production' as suggested in this issue) but I receive this error during gatsby build script.

Am I missing something? How to use this lib in a GatsbyJs app?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
BayanBennettcommented, Jan 24, 2021

Just throwing this on here just in case anyone is stuck and they happen to have the same problem that I did.

❌Doesn’t work

module.exports = {
  wrapRootElement: ({ element }) =>
    createElement(RecoilRoot, null, element);
}

✔ Works

module.exports.wrapRootElement = ({ element }) =>
    createElement(RecoilRoot, null, element);

Also, to reduce duplication, you can do this in gatsby-browser.js:

module.exports.wrapRootElement = require("./gatsby-ssr").wrapRootElement
1reaction
luis-aguilar-rawengcommented, Sep 29, 2020

@risakanagawa For sure, hope this helps.

First, know that the gatsby-browser file “…lets you…wrap your site in additional components…” - https://www.gatsbyjs.com/docs/browser-apis. You need to wrap your root element to allow passing the recoil state, for example:

exports.wrapRootElement = ({ element }) => {
  
  return (
      <RecoilRoot>
         { element }
      </RecoilRoot>
  )
}

Second, if you have the gatsby-ssr file then you pretty much need to do the same since “…There is an equivalent hook in Gatsby’s Browser API. It is recommended to use both APIs together.” - https://www.gatsbyjs.com/docs/ssr-apis/

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS error is "This component must be used inside a ...
To use Recoil (A state management library for React) properly You have to add RecoilRoot wrap component(s). As we can read in documentation ......
Read more >
This component must be used inside a <RecoilRoot ...
Hi, I followed the Quick start tutorial (https://docs.onflow.org/flow-js-sdk/flow-app-quickstart). I end up getting an error: This component ...
Read more >
How to Use Recoil.js Library in Gatsby - Medium
RecoilRoot component is placed inside the wrapPageElement function, which is then exported from the gatsby-browser.js file. Gatsby · Gatsbyjs.
Read more >
Part 2: Use and Style React Components - Gatsby
Import and use a pre-built component from another package. Create your own reusable “building block” component. Use component props to change the way...
Read more >
How to Use Recoil.js Library in Gatsby - Morioh
To use Recoil library to persist state between components, RecoilRoot component needs to be placed at the top of the component tree.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found