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.

Theme object not provided by ThemeProvider when using a component library

See original GitHub issue

Current behavior:

If ThemeProvider is imported into a component library, re-exported and packaged, the theme object doesn’t seem to be available to components in a project that consumes the library.

I’m honestly not sure if this is a bug or something I’m doing wrong. If it’s a config problem at my end I’m sorry.

To reproduce:

Minimal repo here https://github.com/Vazerthon/node-monorepo-test

Node 15 required!

  • Clone the repo and follow guide in README
    • npm install in common/ui then npm run build
    • npm install at top level (to make shared lib available)
    • npm install in apps/simple and npm start
    • Uncomment any use of theme in App.js

Expected behavior:

The above steps should show that the theme is never provided to components in the React tree despite ThemeProvider being used and dev tools showing that the context is in place.

Environment information:

In common/ui “react”: “^17.0.1”, “react-dom”: “^17.0.1”, “webpack”: “^5.6.0”,

In apps/simple “react”: “^17.0.1”, “react-dom”: “^17.0.1”, “react-scripts”: “4.0.1”,

~ Edit ~

Upgrading to Emotion 11 has made the problem different / worse. It’s no longer limited to just the theme, any use of emotion in the client project causes a React hooks error.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Dec 23, 2020

I believe that you should not have to install in each workspace - nor of them should have its own package-lock. THe root acts as an aggregate for the monorepo. This seems to be confirmed by some example that I have found in the wild and reflects what I’m used to from other monorepo setups: https://github.com/Quramy/npm-ts-workspaces-example/tree/0358e4cc84a5bea03604d2dafe4bb1eefc0af1f9

npm install in the root makes your packages available in the monorepo but your library has to also be built before it can actually be used by your app.

1reaction
Andaristcommented, Dec 23, 2020

99,9% of such cases are caused because you are using a duplicate @emotion/react dependency - so you provide from one copy but consume from another one. Please inspect your node_modules, npm link setup (if applicable) and so on

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changing Supplied Value to ThemeProvider is not causing ...
Note: I want to clear , Main Project is using a React Component Library which has theme obejct via ThemeProvider. Now I want...
Read more >
Theming - Material UI - MUI
ThemeProvider relies on the context feature of React to pass the theme down to the components, so you need to make sure that...
Read more >
Context - React
Context provides a way to pass data through the component tree without having to ... React will find the closest theme Provider above...
Read more >
How it Works - Theme UI
The Theme UI ThemeProvider includes a default components object with styled components that pick up values from the theme.styles object.
Read more >
Theming | Amplify UI for React
You can theme Amplify UI using CSS and CSS variables if you do not want to use the theme object structure. Amplify UI...
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