Call Function OnPress React Native


Answer :

In you're render you're setting up the handler incorrectly, give this a try;

 <View>        <Icon         name='heart'         color={this.state.myColor}         size= {45}         style={{marginLeft:40}}         onPress={this.handleClick}                                          />   </View> 

The syntax you're using would make sense for declaring an anonymous function inline or something but since your handler is defined on the class, you just reference it (not call it) using this.functionName in the props.


A little late to the party, but just wanted to leave this here if someone needs it

export default class mainScreen extends Component {  handleClick = () => {  //some code }  render() {  return(   <View>        <Button         name='someButton'         onPress={() => {             this.handleClick(); //usual call like vanilla javascript, but uses this operator          }}                                          />   </View>   )}; 

You need to reference the method with the this keyword.

 <Icon     onPress={this.handleClick}                                    /> 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?