0

new to React Native, coming from React.js. I'm trying to emulate CSS-like cascading by passing on properties, in this case colors, to a component. However, even though everything seems fine, no colours are passed to the Header component. How can I alleviate the problem? App.tsx:

export type Colors = {
  backgroundLight: string,
  mainLight: string,
  secondaryLight: string,
  tertiaryLight: string,
  commentLight: string,
}

const COLORS: Colors = {
  backgroundLight: '#F6F6F2',
  mainLight: '#388087',
  secondaryLight: '#6FB3B8',
  tertiaryLight: '#BADFE7',
  commentLight: '#C2EDCE',

}

return (
    <SafeAreaView style={backgroundStyle}>
      <StatusBar
        barStyle={'dark-content'}
        backgroundColor={backgroundStyle.backgroundColor}
      />
      <Header colors={COLORS}/>
//...
    </SafeAreaView>

Header.tsx:

import { Text, StyleSheet } from "react-native"
import { Colors } from '../App'

const Header = ({mainLight} : Colors) => {
    return (
        <Text style={[
            styles.appTitle,
            {
                color: mainLight,
            },
        ]}>Header</Text>
    )
}

const styles = StyleSheet.create({
    appTitle: {
        fontWeight: '800',
        fontSize: 32
    },
});


export default Header

1 Answer 1

1

You're destructuring mainLight from your props not colors ( the hierarchy of props would be: props->colors->mainLight ), try this:

const Header = ({colors} : {colors: Colors}) => {
    return (
        <Text style={[
            styles.appTitle,
            {
                color: colors.mainLight,
            },
        ]}>Header</Text>
    )
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.