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.

withStyles doesn't inject styles correctly

See original GitHub issue

Unexpected Behavior

  • [x ] bug report
  • feature request

Issue description

Current behavior: I created a wrapper for TopNavigation to auto display a back button as leftControl if the user can go back in navigationStack. So in such case, the leftControl would be:

leftControl = (
        <View>
          <TouchableOpacity onPress={() => goBack()}>
            <Feather name="chevron-left" size={32} color="white" />
          </TouchableOpacity>
        </View>
      );

Then I exported my component as following:

export const NavigationBar = withStyles(NavigationBarComponent, (theme: ThemeType) => ({
  color: theme['color-primary-default'],
}));

This should inject a themedStyle Object in my component’s props, that has a “color” property, which currently, I’m getting like this:

color: {
 "0": "$",
"1": "c",
"10": "m",
"11": "a",
"12": "r",
"13": "y",
"14": "-",
"15": "5",
"16": "0",
"17": "0",
"2": "o",
 "3": "l",
"4": "o",
"5": "r",
"6": "-",
"7": "p",
"8": "r",
"9": "i",
}

Expected behavior: “color” property should hold a value of e.g. “#f76a97”

Steps to reproduce: export a component wrapped with “withStyles” and check the themedStyle prop

Related code:

import React from 'react';
import { SafeAreaView, TouchableOpacity, View } from 'react-native';
import { NavigationScreenProp } from "react-navigation";
import { TopNavigation, TopNavigationProps, Layout, withStyles, ThemedComponentProps, ThemeType } from 'react-native-ui-kitten';
import { Feather } from '@expo/vector-icons';

interface NavigationBarProps extends TopNavigationProps, ThemedComponentProps {
  renderBackButton?: boolean;
  navigation?: NavigationScreenProp<any,any>;
}

const NavigationBarComponent = (props: NavigationBarProps) => {

  // Checking If There Should Be a Back Button As Left Control ------
  let leftControl: React.ReactElement | undefined;
  if (props && props.renderBackButton && props.navigation) {
    const parent: NavigationScreenProp<any, any> | undefined = props.navigation.dangerouslyGetParent();
    if (parent && parent.state && parent.state.index && parent.state.index > 0) {

      console.log(props.themedStyle.color);

      // Create GoBack Function -----
      const goBack: Function = () => {
        if (props.navigation) {
          props.navigation.goBack();
        }
      }

      // Create Back Button, left Control Element -----
      leftControl = (
        <View>
          <TouchableOpacity onPress={() => goBack()}>
            <Feather name="chevron-left" size={32} color="white" />
          </TouchableOpacity>
        </View>
      );
    }
  }

  // Create Default Props -----
  const defaultProps: NavigationBarProps = {
    alignment: "center",
    leftControl: leftControl
  };

  // Return Element -----
  return (
    <Layout level="1">
      <SafeAreaView>
        <TopNavigation {...{...defaultProps, ...props}} />
      </SafeAreaView>
    </Layout>
  );
};

// Export Component With Style Props From Theme ----
export const NavigationBar = withStyles(NavigationBarComponent, (theme: ThemeType) => ({
  color: theme['color-primary-default'],
}));

Other information:

Device: iPhone Xs Max
Device OS: iOS 12.3.1
Expo SDK: 33
Ui-Kitten: 4.0.1
@eva: 1.0.0
typescript: 3.4.5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

6reactions
DaniShalashcommented, Jun 20, 2019

@artyorsh Thanks to the kitten tricks app I was able to track and fix the issue. It was solved by simply replacing:

export const NavigationBar = withStyles(NavigationBarComponent, (theme: ThemeType) => ({
  color: theme['color-primary-default'],
}));

With

export const NavigationBar = withStyles(NavigationBarComponent, (theme: ThemeType) => ({
  backButton: {
    color: theme['color-primary-default'],
  }
}));

Thank you very much for the quick replies

2reactions
artyorshcommented, Dec 16, 2019

@YashGadle take more attention on the @DaniShalash answer. withStyles arrow function should return an object with nested objects (not single style entries), just like you do when using StyleSheet

It works because it iterates through the keys of object you return

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material-UI withStyles doesn't apply any kind of styles
makeStyles returns a custom hook and passing this custom hook into withStyles will not work correctly. Instead, you want the following:
Read more >
style-loader - webpack
singletonStyleTag. Automatically injects styles into the DOM using one <style></style> . Source maps do not work. import styles from "./styles.
Read more >
CSS in JS - Material-UI
We use the higher-order component created by withStyles to inject an array of styles into the DOM as CSS, using JSS. Here's an...
Read more >
Why Your Custom CSS Doesn't Work in WordPress and How ...
Occasionally, you may find that, when you add custom CSS to your website, it just doesn't seem to get applied correctly.
Read more >
What is Material-UI and how to use it correctly? - Tabnine
Here, we're using the withStyles HOC to inject a classes prop into our CustomAppBar component. We can then use the classes object to...
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