如何将在 textinput 中输入的值传递给 url?

how to pass value entered in textinput to the url?

大家好,我正在尝试使用后端 API 在 React Native 中创建搜索,我必须将输入到 TextInput 中的单词传递给 url。我不确定我这样做是否正确,任何人都可以帮助我纠正

这是代码。

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)} />

您尝试使用的是 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals

它应该是这样的:

fetch(`http://www.endpoints.com/search/${this.state.search}`

好像是把{this.state.search}作为字符串

改变

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

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