cbll
cbll

Reputation: 7219

React-native const component doesn't receive props?

Coming from React, I thought passing props was the same, apparently it is not?

I have a login form in which I'd like to have a sign in, and sign up button with different styling.

In my loginform, I have the following method:

   renderButton (type, text) {
        if (this.state.loading) {
            return <Spinner size="small" />;
        }

        let btnColors = {
            bgColor: colors.whiteText,
            textColor: colors.primaryTeal
        };

        if (type === "signUp") {
            btnColors.bgColor = colors.whiteText;
            btnColors.textColor =  colors.primaryTeal;

        } else if (type === "logIn") {
            btnColors.bgColor = colors.darkTeal;
            btnColors.textColor = colors.whiteText;
        }
        return (
            <Button colors={btnColors} onPress={this.onButtonPress.bind(this)}>
                {text}
            </Button>
        );
    }

Invoked in Render by:

{this.renderButton("signUp", "SIGN UP")}

Button.js looks like:

import React from 'react';
import { Text, TouchableOpacity, View, StyleSheet} from 'react-native';


const Button = ({colors, onPress, children }) => {

    const styles =  StyleSheet.create({
        textStyle: {
            alignSelf: 'center',
            color: colors.textColor,
            fontSize: 16,
            fontWeight: '900',
            paddingTop: 10,
            paddingBottom: 10,
        },
        buttonStyle: {
            flex: 1,
            backgroundColor: colors.bgColor,
            borderRadius: 50
        },
    });

      return (
            <TouchableOpacity onPress={onPress} style={styles.buttonStyle}>
              <Text style={styles.textStyle}>
                  {children}
              </Text>
            </TouchableOpacity>
      );
};


export { Button };

With the error:

undefined is not an object (evaluating 'colors.textColor')

Why doesn't it work, and how would I conditionally pass props as an object to use for styling?

Upvotes: 0

Views: 891

Answers (1)

ThatCoderGuy
ThatCoderGuy

Reputation: 677

Try changing the component to a class component instead of a functional component, this should do the trick... alternatove ypu can use the fat arrow syntax as shown below:

const HelloWorld = ({name}) => ( <div>{`Hi ${name}`}</div>);

Upvotes: 1

Related Questions