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.

Typescript: useContext(ThemeContext) doesn't expose extended theme types

See original GitHub issue

My 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:closed
  • Created 3 years ago
  • Comments:21 (11 by maintainers)

github_iconTop GitHub Comments

7reactions
walterholohancommented, Sep 29, 2020

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 below

e.g.

import {Colors, FullTheme} from 'react-native-elements'

type RecursivePartial<T> = {[P in keyof T]?: RecursivePartial<T[P]>}

declare module 'react-native-elements' {
  export interface Colors {
    background: string
    darkText: string
  }

  type ThemeFontDimention = {
    fontSize: number
    lineHeight: number
  }

  export interface FullTheme {
    colors: RecursivePartial<Colors>
    padding: {
      small: number
      default: number
      large: number
      screen: number
    }
    fontSizes: {
      small: ThemeFontDimention
      medium: ThemeFontDimention
      large: ThemeFontDimention
    }
  }
}
4reactions
brerdemcommented, Jul 21, 2020

@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?

image

Read more comments on GitHub >

github_iconTop 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 >

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