Function with React Native parameters

Asked

Viewed 520 times

-1

I got my construtor:

constructor(props) {

        super(props);

        this.state = {
            categorias: [],
            refreshing: true
        }

        this.buscarSubcategorias = this.buscarSubcategorias.bind(this);
    }

And the function:

buscarSubcategorias(categoria_id) {
        alert(categoria_id);
    }

And on my button I have:

<TouchableHighlight key={ item.id } onPress={ () => { this.buscarSubcategorias(item.id) }}>
         <Text style={ styles.txtLinks }>{ item.nome }</Text>
</TouchableHighlight>

But when clicking I get the following error:

Undefined is not a Function (evaluating '_this3.searchSubcategories(item.id)')

2 answers

0

Since you are using an Arrow Function as a callback, it is not necessary to give a bind in the constructor. Remove the bind line and it will probably work.

  • Yes, this responds. If it removes . bind in the constructor it ceases to receive Undefined.

0


It is not necessary to bind the function. It is also not necessary to use the keys when calling the function:

onPress={ () => { this.buscarSubcategorias(item.id) } }

Without them:

onPress={ () => this.buscarSubcategorias(item.id) }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.