Search code examples
urlsearchreact-nativebackendtextinput

how to pass value entered in textinput to the url?


Hey guys i am trying to create search with back-end API in react native and i have to pass the word entered into the TextInput to the url. I am not sure whether i am doing it correctly or not can any body help me in rectifying

Here is the code.

this.state = {
      search: "",
    }

async onSearchPressed() {
    try {
      let response = await fetch("http://www.endpoints.com/search/{this.state.search}", {
        method: "GET",
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
      }); 


      render = () => {
    let fields = [
            {ref: 'search', placeholder: 'search', keyboardType:'default',secureTextEntry: false},];
     return (
       <TextInput
          {...fields[0]}
          onChangeText={(val) => this.setState({search: val})}
          value={this.state.search}
        />

       <TouchableOpacity onPress={this.onSearchPressed.bind(this)} />

Solution

  • It looks like, It taken the {this.state.search} as the string.

    Change

    let response = await fetch("http://www.endpoints.com/search/{this.state.search}", {
    

    to

    let response = await fetch("http://www.endpoints.com/search/"+this.state.search, {