ThemeContext missing for external components
See original GitHub issueExternal Component
emotion
version: 10.0.2react
version: 16.4.2
I know extending semantic ui is gross, WIP ripping it out
import React from 'react';
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { Container } from 'semantic-ui-react';
const PageTitleContainer = styled.div`
background: ${props => props.theme.colors.whiteHex} !important;
padding: ${props => props.theme.pageTitle.titleContainerPadding} !important;
border-bottom: ${props => props.theme.pageTitle.pageTitleBorder(props)} !important;
h1 {
font-size: ${props => props.theme.pageTitle.h1.fontSize} !important;
font-weight: ${props => props.theme.pageTitle.h1.fontWeight} !important;
margin: ${props => props.theme.pageTitle.h1.margin} !important;
padding: ${props => props.theme.pageTitle.h1.padding} !important;
color: ${props => props.theme.colors.lightBlueHex} !important;
}
`;
const FlexContainer = styled(Container)`
display:flex !important;
justify-content: space-between;
`
const ChildContainer = styled.div`
display:flex;
justify-content: space-between;
&> div {
&:first-of-type {
margin-left: 0;
}
margin-left:15px;
}
`
export const PageTitle = props => (
<PageTitleContainer>
<FlexContainer>
<h1>{props.title}</h1>
<ChildContainer>{props.children}</ChildContainer>
</FlexContainer>
</PageTitleContainer>
);
PageTitle.propTypes = {
title: PropTypes.string
};
export default PageTitle;
Consuming Application
emotion
version: 10.0.2react
version: 16.6.3
ReactDOM.render(
<ThemeProvider theme={theme}>
<PageTitle title="test"></PageTitle>
</ThemeProvider>,
document.getElementById('root')
);
What happened:
Reproduction:
- Create external component
- Create external theme object
- Import both into CRA v2
- Create a local component in CRA
- Use ThemeProvider to provide theme prop
At this point the local component of your application should pick up the ThemeContext while the imported component will have an empty object for theme.
Problem description:
The component from our external library does not get the ThemeContext from the ThemeProvider and falls back to the default empty object. This was working prior to upgrading to emotion 10.x.x and worked sporadically using 10.x.x
The theme is also imported from our component library and can be used normally with styled
components that are creating within the consuming application but it will not apply to the components that are imported en tandem with the theme.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
reactjs - React: useContext, how to retrieve it from an external ...
what am I missing? EDIT: Based on the comments, I use a third file and import the context to get access to it...
Read more >Advanced Usage - styled-components
If you ever need to use the current theme outside styled components (e.g. inside ... This is useful to circumvent a missing ThemeProvider...
Read more >How to pass data around using Reacts new Context API
Using Context API, we can do this really simple without having to pass props down the component tree. Creating the context. The first...
Read more >context-component - npm Package Health Analysis | Snyk
Creating ContextComponent ; from 'context-component'; export ; default class ThemeContext extends ContextComponent { state = {theme ; 'dark'} ...
Read more >ThemeContext | Stream Chat - React Native SDK Docs
ThemeContext can be consumed by any of the child component of OverlayProvider or Chat components. import { useContext } from 'react' ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @HiranmayaGundu,
I finally figured out my issue, which may be yours as well. Going back to what @Andarist said, the newer Context API is identity based. The solution was to export the
ThemeProvider
from our component library itself instead of trying to useemotion-theming
directly in the consuming application.Use the
ThemeProvider
from@miq/fiber-ui
– if it’s an internal package then just update it to export your provider.Follow up, learned more.
The problem was indeed two versions of the theming library.
Required:
Using rollup:
Using webpack for bundling a library: This is untested, just filling out based on documentation below. https://webpack.js.org/configuration/externals/#string
Using webpack for bundling an application: This is untested, just filling out based on documentation below. https://webpack.js.org/configuration/resolve/#resolvealias