Typescript: useContext(ThemeContext) doesn't expose extended theme types
See original GitHub issueMy theme looks something like this:
const theme = {
colors: {
primary: '#fff'
},
customColors: {
background: '#000'
}
}
Referencing an extended theme with const { theme } = React.useContext(ThemeProvider)
does not expose the extended theme elements in typescript - in this example, typescript does not know about the customColors
object on the theme.
Copying @braincore from #2007, it looks like this is because the ThemeContext statically defines the generic in ThemeProps as {}. And a workaround is to use a type assertion: const { theme } = useContext(ThemeContext) as ThemeProps<MyTheme>;
Is this workaround the recommended way to do this? Or is it possible to define the ThemeContext in some way after the defaultTheme is defined by deep merging the base theme with the custom theme?
Envi details:
software | version |
---|---|
react-native-elements | ^1.2.7 |
react-native | 0.61.4 |
Issue Analytics
- State:
- Created 3 years ago
- Comments:21 (11 by maintainers)
Top Results From Across the Web
How to use React Context with TypeScript - LogRocket Blog
We create the types necessary for implementing theming: Theme specifies the possible theme modes and ThemeContextType specifies the properties ...
Read more >React TypeScript UseContext with complex type does not work
The ThemeContext example from this link works, but it only serves one field. I tried to use this example for a complex type...
Read more >Advanced React Context Typescript | useContext ... - YouTube
Time to talk about context again! This time it's more advanced and I'll talk about using ReactJS Context with Typescript.
Read more >Manage design tokens with TypeScript and styled-components
In order to keep this article easy to understand, we will focus on two token types: colors (primary / secondary); spaces (margins /...
Read more >React & Redux in TypeScript Complete Guide
Gets Props type of a specified component XXX (WARNING: does not work ... context/theme-context'; type Props = {}; export default function ...
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
Same here, in order to resolve the issue I had to add
import {Colors, FullTheme} from 'react-native-elements'
to the top of the file. Example of my declaration file belowe.g.
@Johan-dutoit thanks it worked. However now I’m getting error about every element that I didn’t declare in the file (see the screenshot below) Shouldn’t it be merged into the existing definitions?