嘿伙计们,我正在尝试使用后端API在本机中创建搜索,我必须将输入的单词传递给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)} />
答案 0 :(得分:1)
看起来,它将{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, {
答案 1 :(得分:0)
您尝试使用的是https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals。
它应该像:
function addNewLines(str) {
var finalString = '';
for(var i=0; i < str.length; i++) {
finalString += str.substring(0, 64) + '\n';
str = str.substring(64);
}
finalString += str;
return finalString;
}