簡體   English   中英

反應本機可觸摸不透明度功能

[英]React Native Touchable Opacity functions

我的React Native應用程序中有兩個函數,按下時需要我的TouchableOpacity來調用它們。 為此,我嘗試在onPress方法中簡單地使用帶有兩個函數的箭頭函數,但這是行不通的。 我認為這與范圍有關,但我不確定。 僅將其單獨傳遞給onPress方法時,這兩個函數即可正常工作。 這是代碼(為了便於閱讀,我對其進行了很多裁剪)請幫忙。

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>
)
}

您錯過了函數的括號

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM