2017-03-13 3 views
3

は、私はシンプルなアイコン・ボタンを持っているリアクト:は、次のようにネイティブTouchableOpacityたonPress問題

class SideIcon extends Component { 
    render() { 
    return (
     <TouchableOpacity onPress={console.log('puff')} style={styles.burgerButton}> 
     <Icon name="bars" style={styles.burgerIcon}/> 
     </TouchableOpacity> 
    ); 
    } 
} 

それは、以下のコンポーネントから呼ばれています:

export default test = React.createClass({ 
    getInitialState: function() { 
    return { 
     isModalOpen: false 
    } 
    }, 

    _openModal() { 
    this.setState({ 
     isModalOpen: true 
    }); 
    }, 

    _closeModal() { 
    this.setState({ 
     isModalOpen: false 
    }); 
    }, 
    render() { 
    return (
    <View style={styles.containerHead} keyboardShouldPersistTaps={true}> 
    **<SideIcon openModal={this._openModal} closeModal={this._closeModal} />** 
     <Text style={styles.logoName}>DareMe</Text> 
     <SideBar isVisible={this.state.isModalOpen} /> 
    </View> 
    ); 
    } 
}); 

しかしTouchableOpacityonPressは決して機能しません。どこが間違っているの?コンポーネントロード中にコンソールステートメントを表示しますが。

答えて

4

コードを呼び出す関数をバインドする必要があります。例えば

<TouchableOpacity onPress={() => console.log('puff')} style={styles.burgerButton}> 
    <Icon name="bars" style={styles.burgerIcon}/> 
</TouchableOpacity> 

良い方法は、それを成分関数

class SideIcon extends Component { 
    handleOnPress() { 
    console.log('puff') 
    } 
    render() { 
    return (
     <TouchableOpacity onPress={this.handleOnPress} style={styles.burgerButton}> 
     <Icon name="bars" style={styles.burgerIcon}/> 
     </TouchableOpacity> 
    ); 
    } 
} 
への参照を与えることです
関連する問題