How to take the parameter passed in the function and take the same function in React-Native?

Asked

Viewed 511 times

-1

Hello

I want to pass a value for a function in a touchableOpacity and take this value, within the function, as I should proceed?

Follows my codes:

import React, { Component } from 'react';

import { View, StyleSheet, TextInput, Text, TouchableOpacity, AsyncStorage, ImageBackground } from 'react-native';

export default class Categorias extends Component {

  constructor(props){
    super(props)

    this.state = {
      cidade: null,
      tipo: "",
    };
  }

  procurar(){
    let cidade = this.state.cidade;
    let tipo = this.state.tipo;
    let opcao = 'procurar estabelecimentos';
    console.log(`${cidade} - ${tipo} - ${opcao}`)
    var urlProcurar = endpoint.backendUrl + 'app/procurarEstabelecimento.php?cidade=' + cidade + '&opcao=' + opcao + '&tipo=' + tipo;
    fetch(urlProcurar,{ method: 'GET'})
     .then((response) => response.json())
     .then((responseJson) => {
        console.log(responseJson);
        this.setState({estabelecimento: responseJson});

    })
       .catch((error) => {
       console.error(error);

       });
  }

  render() {
     return (
      <View style={styles.container}>
        <View style={{marginBottom:20}}>
            <View>
                <Text style={styles.textoCategorias}>Categorias</Text>
            </View>
            <View>
                <TextInput style={styles.inputProcurarCidade}
                    placeholder="Cidade"
                    onChangeText={texto => this.state.cidade = texto}
                />
            </View>


        </View>
          <TouchableOpacity style={{marginBottom:20}} onPress={ ()=>{ this.procurar() && this.setState({tipo: "Restaurante"}) } }>
              <ImageBackground style={styles.restaurante} source={restaurantes}>
              <Text style={styles.textoRestaurante} >R E S T A U R A N T E S</Text>
              </ImageBackground>
          </TouchableOpacity>


      </View>
    );
  }
}


})

1 answer

0

I got...

On the button do:

<TouchableOpacity style={{marginBottom:20}} onPress={ ()=>{ this.procurar('Restaurante') } }>

And not function:

 procurar(arg){
   let cidade = this.state.cidade;
   let opcao = 'procurar estabelecimentos';
   console.log(`${cidade} - ${arg} - ${opcao}`)
 }

Browser other questions tagged

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