Use if in onPress event

0

I'm trying to use a if like this:

<Button
      iconRight={{
        name: 'search',
        type: 'font-awesome'
      }}
      title='BUSCAR'
      buttonStyle={{
        marginTop: 15,
        borderRadius: 7,
        backgroundColor:'#2C5CAA'
      }}
      onPress={(function() {if (this.state.busca.length > 0){
        alert(this.state.busca.length);
      }else{
        this.props.navigation.navigate('Lista', {
                                                              busca: this.state.busca,
                                                              city: this.state.city })}
      })
    }
     />

However, I'm getting an error in the variable this.state.busca :

  

undefined is not an object (evaluating 'this.state.search')

How to solve?

    
asked by anonymous 28.01.2018 / 21:41

1 answer

0
Most likely in% with this.state.busca is bound to the context of its anonymous function within this try to make a onPress() to pass the context of its class to bind() :

<Button

  iconRight={{
    name: 'search',
    type: 'font-awesome'
  }}

  title='BUSCAR'

  buttonStyle={{
    marginTop: 15,
    borderRadius: 7,
    backgroundColor:'#2C5CAA'
  }}

  onPress={(function() {
    if (this.state.busca.length > 0){
        alert(this.state.busca.length);
    } else {
        this.props.navigation.navigate('Lista', {
            busca: this.state.busca,
            city: this.state.city 
        })
    }
  }).bind(this)

} />

I recommend that you isolate this function in another part of your code for better readability.

    
29.01.2018 / 18:46