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.

[feature request] useTheme hook

See original GitHub issue

Issue type

I’m submitting a …

  • bug report
  • feature request

Issue description

Current behavior: Not implemented yet.

Expected behavior: Exposing a custom hook useTheme to access to theme context value.

Using this hook we can be able to access the current value theme in this manner:

e.x:

import React from 'react';
import { View } from 'react-native';
import { useTheme } from 'themes';

function MyComponent() {
  const themeValue = useTheme();
  const backgroundColor = React.useMemo(
    () =>
      themeValue === 'light'
        ? { backgroundColor: 'blue' }
        : { backgroundColor: 'darkblue' },
    [themeValue]
  );

  return <View style={[backgroundColor]}></View>;
}

Related code:

Adding a useContext consuming ThemeContext.

export function useTheme(): ThemeType {
  const themeContext = useContext(ThemeContext);

  if (isEmpty(themeContext)) {
    throw new Error('useTheme must be used within a ThemeProvider');
  }

  return themeContext;
}

Other information:

I have a fork with useTheme implemented with tests, but im not able to start expo app -.- It’s not finished yet but if maintainers are agree and see useful this feature im glad to keep working on this pr. link to repo with useTheme: https://github.com/x0s3/react-native-ui-kitten

Im open to talk else feel free to close this feature request 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:11

github_iconTop GitHub Comments

2reactions
artyorshcommented, Dec 18, 2019

@henoktsegaye when using withStyles in component you can do

const backgroundColor = props.themedStyle.container.backgroundColor;
2reactions
artyorshcommented, Oct 16, 2019

@Veikedo We also have already some scripts to generate typescript interfaces for theme, component parameters and other Eva-based things, so it can make work autocomplete in your IDE, but it is also a beta feature

I also want to answer one question that you probably might have: We use dash-case for naming variables because we also have a web framework that is also based on Eva Design System 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

useTheme - React Navigation
The useTheme hook lets us access the currently active theme. You can use it in your own components to have them respond to...
Read more >
useTheme React Hook - useHooks
Hooks are a feature in React that allow you use state and other React features without writing classes. This website provides easy to ......
Read more >
Mocking React hooks when unit testing using Jest
Below I mock the base-fetch module which is responsible for making requests to the SWAPI endpoints and returning a JSON object. Instead of ......
Read more >
Forget Everything you Learned about React - Hooks Rock!
MaterialUI has provided a custom hook called useTheme to get access to the theme within the component. Next, we call useState to set...
Read more >
Using the useResource Hook - Learn React Hooks [Book]
A more powerful way of dealing with requests, is using the axios and react-request-hook libraries. Using these libraries, we have access to features...
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