Zeeshan Ali Khan
Zeeshan Ali Khan

Reputation: 141

How can I pass function call in a component as a prop in React Native?

I am trying to pass my function updateProfile as a prop in the Button component shown below. The function is not working and the application crashes when I press the button.

Here is the code of button component.

const Button = (props) => {         
    return (            
         <>
            <TouchableOpacity
                style={styles.buttonContainer}
                disabled={props.disabled}
                onPress={props.functionCall}
            >
                <Text style={styles.buttonText}>{props.title}</Text>
            </TouchableOpacity>             
        </>         
    );  
 };

And here is how I am using

<Button disabled={isLoading ? true : false} functionCall="updateProfile" title="Edit Profile"/>

The function

const updateProfile = () => {
    navigation.navigate("updateProfile", member);
};

Actually, I have many buttons and each button has different function call. So, it needs to be sented through props. Help needed!

Upvotes: 0

Views: 870

Answers (1)

Tayyab Mazhar
Tayyab Mazhar

Reputation: 1712

Change this:

functionCall="updateProfile"

to this:

functionCall={updateProfile}

Upvotes: 1

Related Questions