【问题标题】:React-Native onPress doesn't work, when touch the icon触摸图标时,React-Native onPress 不起作用
【发布时间】:2018-11-30 19:29:31
【问题描述】:

我正在使用react-native-element 创建一个button group,它嵌入了来自react-native-vector-icons 的图标。

问题是当图标被触摸时,onPress 没有被触发

    constructor(props) {
      super(props);    
      this.state = { selectedIndex: 0 };   
      this.SetSelected = this.SetSelected.bind(this);
    }


    SetSelected(index) {
      this.setState({ selectedIndex: index });
    }

    return(
    <ButtonGroup
      selectedIndex={this.state.selectedIndex}
      onPress={this.SetSelected}
      selectedButtonStyle={{ backgroundColor: 'blue' }}
      buttons={[
        {
          element: () => (
            <Icon.Button
              name="slack"
              style={{ backgroundColor: 'white' }}
              color={'black'}
              size={30}
              title="Inbox"
            >
              <Text style={{ color: 'black', fontSize: 15, textAlignVertical: 'center', textAlign: 'center' }}                   
              >
                All
              </Text>
            </Icon.Button>
          ),
        })

【问题讨论】:

    标签: react-native react-native-vector-icons


    【解决方案1】:

    Thanks to Kyle Roach,

    原因是 因为我使用的是可触摸的Icon.Button。因此,当我尝试点击更改 ButtonGroup 时,触摸事件将被 Icon.Button 捕获,而不是 buttonGroup 的按钮。

    我应该使用Icon 而不是Icon.Button

    【讨论】:

      【解决方案2】:

      试着把它变成一个函数。

      onPress={() => {this.SetSelected()}}
      

      如果不起作用,请提供 this.SetSelected 函数。

      【讨论】:

        猜你喜欢
        • 2023-01-22
        • 2018-09-19
        • 2018-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-10
        • 2019-02-18
        • 1970-01-01
        相关资源
        最近更新 更多