New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Empty theme context for 'external' components #2379
Comments
Goddammit i solved it. I studied the approach from @reakit and learned that importing styled-component twice causes different instances (importing for me is not attached to creating instances, maybe thats the reason it was so hard for me to understand). So i solved it by creating a package that imports and exports styled-components like import { ThemeProvider } from 'styled-components';
export { ThemeProvider } and then reuse this package in the App like import { ThemeProvider } from 'my-own-package'; after this the theme prop is filled with the theme props from the ThemeProvider. |
I have exact same problem, Is there anything else you did to make it work? |
@probablyup - The solution provided by @adriankremer did not work for me. Could we reopen this issue? |
Actually - this helped my issue: https://www.styled-components.com/docs/faqs#how-can-i-fix-issues-when-using-npm-link-or-yarn-link |
I'm in a similar situation and, like @Iulia-Soimaru, the proposed solution to create a file that imports and exports ThemeProvider didn't work. I then tried the proposed solution of adjusting my webpack config with something like
and also didn't get any further along. My theme object in the external components props is still empty. Is there anything more specific that you did @Fluffy-Samurai ? |
If you're using yarn, the "resolutions" package.json field is usually more reliable than the webpack alias in my experience |
@JeuelyFish I fixed this issue by wrapping external component in Component from external package:
using external component in my application:
|
For those that may be having issues with their own external library, the following worked for me. Make styled-components an external dependency of your component library. (Documentation). This ensures that your component library uses the same instance of the |
You beautiful golden child @nlicitra!!! Indeed, this was the fix for myself. |
None of these suggestions worked for me but here's what did: Component's Library:
Application:
This forces both applications to use the linked version which SHOULD be the same. I wrote a little script for myself to run these commands on the projects so it's not as hacky as it seems. |
👍 @nlicitra, @Fluffy-SamuraiI'm faced this issue in a monorepo. There were multiple packages which had different versions of I set the same version of package-1 "styled-components": "^4.2.1", packages-2
I updated all to something like this: "styled-components": "^5.0.0" |
Still facing the same issue here: https://stackoverflow.com/questions/63327130/undefined-props-theme-when-using-local-component-library-in-another-application Tried the solutions above but nothing seems to work, would appreciate if someone can see if I'm dong something wrong in my setup. |
Not sure why this was closed... It's definitely still an issue. |
Can you create a repository on GitHub replicating the issue? This way we can clone and debug. Thanks! |
I got it working. I moved styled-components as a peer dep [and into dev deps if you want to run your other package on its own, i.e with storybook etc] and i updated webpack to look for style-components as a dep in the original package my webpack config
package.json
|
@monokrome I think this is not an issue with styled-components itself, but with npm / package.json configurations. If you have dependencies which rely on styled-components, all those dependencies should include styled-components as a only the final app should include This should fix the issues, but I think there is nothing code-wise to do on the styled-components side. maybe just be more clear with this in the docs |
I'm having a similar issue. some styled component created in app: |
j-avila, I had the same issue as yours one. Monorepo with lerna, shared module accepts styled-components as peerDependency from other packages. The problem was resolved by setting styled-components to the same version (^5.3.3 in my case) in all packages. |
Had the same issue. Using monorepo with multiple UI components that has to be customized through a shared theme. lerna bootstrap --hoist |
If anyone is still facing issues with Lerna then the following worked for me:
|
Solved for Next.js project using a separate standalone package which uses Next.js, styled-components, and react. Solved it based on all the wonderful feedback and solutions in this thread.
Here is my /** @type {import('next').NextConfig} */
import createMDXPlugin from '@next/mdx'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const mdxConfig = {
extension: /\.mdx?$/,
options: {
providerImportSource: '@mdx-js/react',
rehypePlugins: [],
// If you use remark-gfm, you'll need to use next.config.mjs
// as the package is ESM only
// https://github.com/remarkjs/remark-gfm#install
remarkPlugins: [],
},
}
const withMDX = createMDXPlugin(mdxConfig)
const nextConfig = withMDX({
compiler: {
styledComponents: true,
},
images: {
domains: ['lancejpollard.com'],
},
pageExtensions: ['mdx', 'md', 'tsx', 'ts'],
poweredByHeader: false,
// reactStrictMode: true,
trailingSlash: false,
webpack: config => {
config.resolve = {
...config.resolve,
alias: {
...config.resolve.alias,
next: path.resolve(__dirname, './node_modules/next'),
react: path.resolve(__dirname, './node_modules/react'),
'react-dom': path.resolve(
__dirname,
'./node_modules/react-dom',
),
'styled-components': path.resolve(
__dirname,
'./node_modules/styled-components',
),
},
extensions: ['.ts', '.tsx', '.js', '.json'],
}
return config
},
})
export default nextConfig Now can develop the standalone package while using the app and refresh the browser, and it reflects the changes! Thank you so much everyone for the help 🥳 Now onto other issues with module loading in this weird setup :)
But it is working for the most part otherwise. |
I'd just like to share in our case (monorepo), we had to apply a combination of the above solutions (and not just one):
Note that for us on (2) it was on the directory above of the current package - i.e. Hope this is helpful to someone in the future 👍 |
…theming environments. reference: styled-components/styled-components#2379
…theming environments. reference: styled-components/styled-components#2379
…theming environments. reference: styled-components/styled-components#2379
…theming environments. reference: styled-components/styled-components#2379
* minor upgrade: styled-components * remap package in jest setup * treat styled-components as peer dependency due to risk of having two theming environments. reference: styled-components/styled-components#2379
Upgrading style components to 5.x.x + fixed this for me. |
Using a ThemeProvider with a theme, components that are separate transpiled modules (in my case UMD) inheriting {props => props.theme..} won't work. Also tried with HoC (withTheme) where i get theme: undefined.
They're just empty.
App
Component (StyledButton)
Tried with NPM and Yarn.
I thought this worked before.
System:
Binaries:
npmPackages:
Steps to reproduce
The easiest way to reproduce this is to setup a lerna monorepo and to add a 'app' that uses a ThemeProvider and a separate 'component' package that invokes ${props => props.theme}.
The text was updated successfully, but these errors were encountered: