【问题标题】:How can I pass function call in a component as a prop in React Native?如何在 React Native 中将组件中的函数调用作为道具传递?
【发布时间】:2021-05-31 11:03:42
【问题描述】:

我正在尝试将我的函数 updateProfile 作为道具传递给如下所示的 Button 组件。该功能不起作用,当我按下按钮时应用程序崩溃。

这里是按钮组件的代码。

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

这就是我的使用方式

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

功能

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

实际上,我有很多按钮,每个按钮都有不同的函数调用。所以,需要通过 props 发送。需要帮助!

【问题讨论】:

  • 尝试像这样传递函数:functionCall={updateProfile}
  • 您将其作为字符串传递..将其作为函数传递给 {}

标签: javascript reactjs react-native frontend mern


【解决方案1】:

改变这个:

functionCall="updateProfile"

到这里:

functionCall={updateProfile}

【讨论】:

  • 谢谢,亲爱的,虽然这是一个小错误,但我试图修复它,但花了我很长时间。
猜你喜欢
  • 2023-03-28
  • 2022-01-24
  • 2021-05-26
  • 1970-01-01
  • 2019-12-31
  • 2018-08-01
  • 2022-01-24
  • 2017-09-06
相关资源
最近更新 更多