6

I am building a mobile app on react-native! Recently I build my own header component for every page. Here is the code of my Header component. I have two icons inside that component. But unfortunately these buttons are not working at all!!

import React, {Component} from 'react';
import { 
    Text, 
    View, 
    Image, 
  Dimensions,
  TouchableOpacity
} from 'react-native';

import Icon from 'react-native-vector-icons/Ionicons';

import Styles from './Styles';

export default class ChatHead extends Component {
  constructor(props) {
      super(props);

      this.state = {
      }
  }

  render(){
    console.log(this.props.headerText, this.props);
    if(this.props.headerText.length > 16){
      name = this.props.headerText.substr(0, 16);
      name += "..."; 
    }
    else name = this.props.headerText;

    return(
      <View style={Styles.viewStyle}>
        <Text style={Styles.nameStyle}>{name}</Text>

        <TouchableOpacity 
          style={Styles.audioCallButton}
          onPress={() => console.log("Audio Button Pressed")}
        >
          <Icon name={'md-call'} size={25} color="white" align='right'/>
        </TouchableOpacity>

        <TouchableOpacity 
          style={Styles.videoCallButton}
          onPress={() => console.log("Video Button Pressed")}
        >
          <Icon name={'ios-videocam'} size={28} color="white" align='right'/>
        </TouchableOpacity>
      </View>
    );
  }
};

onPress is not responding at all!

uraniumreza
  • 71
  • 1
  • 6

3 Answers3

0

Not the case here, but for future readers, it could be because you wrapped one TouchableOpacity inside an other

Simon
  • 6,025
  • 7
  • 46
  • 98
-1

Try this solution, notice I have wrapped your Icon within a view and also the name.

import React, {Component} from 'react';
import { 
  Text, 
  View, 
  Image, 
  Dimensions,
  TouchableOpacity
} from 'react-native';

import Icon from 'react-native-vector-icons/Ionicons';

import Styles from './Styles';

export default class ChatHead extends Component {
constructor(props) {
  super(props);

  this.state = {
  }
}

render(){
 return(
  <View style={Styles.viewStyle}>
     <Text style={Styles.nameStyle}>{this.props.headerText.length > 16 ?
`${this.props.headerText.substr(0, 16)}...` : this.props.headerText}</Text>

    <TouchableOpacity 
      style={Styles.audioCallButton}
      onPress={() => console.log("Audio Button Pressed")}
    ><View>
      <Icon name={'md-call'} size={25} color="white" align='right'/></View>
    </TouchableOpacity>

    <TouchableOpacity 
      style={Styles.videoCallButton}
      onPress={() => console.log("Video Button Pressed")}
    ><View>
      <Icon name={'ios-videocam'} size={28} color="white" align='right'/></View>
    </TouchableOpacity>
  </View>
 );
 }
};
kabangi julius
  • 2,709
  • 2
  • 16
  • 25
-1

try alert in place of console.log to view the data then and there, it might be the case that you missed the location of c

Sagar
  • 1
  • 3