简体   繁体   English

反应本机可触摸不透明度功能

[英]React Native Touchable Opacity functions

I have two functions in my React Native app, and I need my TouchableOpacity to call both when pressed. 我的React Native应用程序中有两个函数,按下时需要我的TouchableOpacity来调用它们。 To do so I tried to simply use an arrow function in the onPress method with both functions inside, but this doesn't work. 为此,我尝试在onPress方法中简单地使用带有两个函数的箭头函数,但这是行不通的。 I assume it's something to do with scope, but I'm not sure. 我认为这与范围有关,但我不确定。 Both functions work correctly when simply passed into the onPress method alone. 仅将其单独传递给onPress方法时,这两个函数即可正常工作。 Here's the code (I've trimmed it a lot for readability) Please help. 这是代码(为了便于阅读,我对其进行了很多裁剪)请帮忙。

export class CreateItem extends React.Component {
constructor(props){
    super(props);
}

sendData = () => {
    itemData = this.state.item;
    this.props.action(itemData); //the action function alters the parent state (this function works fine every other time)
}
render(){
return(
    <TouchableOpacity
    onPress={() => {
    this.sendData;
    this.props.hide; //This function is passed from the parent and works fine in other scenarios 
    }}
    >
        <Text>Add Item</Text>
    </TouchableOpacity>
)
}

you missed the parentheses of functions 您错过了函数的括号

export class CreateItem extends React.Component {
constructor(props){
    super(props);
}

sendData = () => {
    itemData = this.state.item;
    this.props.action(itemData); //the action function alters the parent state (this function works fine every other time)
}
render(){
    return(
        <TouchableOpacity
            onPress={() => {
                this.sendData();
                this.props.hide(); //This function is passed from the parent and works fine in other scenarios 
            }}
        >
            <Text>Add Item</Text>
        </TouchableOpacity>
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native - 如何禁用可触摸的不透明语音 - React Native - how to disable touchable opacity voice 可触摸不透明度在堆栈导航器屏幕中没有响应 - React Native - Touchable Opacity not responding in stack navigator screen - React Native 如何在React Native中为网格视图中的每个项目赋予可触摸的不透明度? - How to give Touchable Opacity for each item in grid view in React Native? 在 FlatList 内 2 到 5 秒后 React Native 可触摸不透明度按下 - React Native touchable opacity pressing after 2 to 5 seconds inside a FlatList onPress不会在本机反应中检测到可触摸不透明的道具 - onPress doesn't detect props of touchable opacity in react native React Native:Touchable Opacity 元素在 iOS 上可点击,但在 Android 上不可点击 - React Native: Touchable Opacity element is clickable on iOS but not Android 如何禁用 react-native-element 的复选框可触摸不透明度? - How to disable react-native-element's checkbox touchable opacity? 当 TextInput 被聚焦时,在可触摸的不透明度上反应原生点击 - React native tap on touchable opacity while TextInput is focused 反应原生布局 - 视图不显示内部可触摸不透明度 - React native layouts - view not showing inside touchable opacity React Native:可滑动和可触摸 - React Native: Swipeable and Touchable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM