[英]React will not pass props to API link
http://codepen.io/beckbeach/pen/mWqrep My codepen http://codepen.io/beckbeach/pen/mWqrep我的代码笔
React will not pass the props to ${this.state.query} in my API link. React不会将道具传递到我的API链接中的$ {this.state.query}。 What am i doing wrong?
我究竟做错了什么?
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
query: ''
}
}
searchFunction() {
fetch('http://api.openweathermap.org/data/2.5/weather?zip=${this.state.query},us&appid=748f643131acee33c207bee1a969f6e3', {
method: 'GET'
}).then((res) => {
res.json().then((data) => {
console.log(data);
})
})
.catch((err) => {
console.log(err);
})
}
render() {
return (
<div>
<h1>Check The Weather!</h1>
<div>
<input type="text" placeholder="Enter Zipcode" value={this.state.query} onChange={event => {this.setState({query: event.target.value})}} />
<button type="submit" onClick={() => this.searchFunction()}>CHECK WEATHER </button>
</div>
</div>
)}
}
ReactDOM.render(<App/>, document.getElementById('root'))
Your using a single quote instead of back ticks for the template literal. 您使用单引号而不是模板文字的反引号。
'http://api.openweathermap.org/data/2.5/weather?zip=${this.state.query},us&appid=748f643131acee33c207bee1a969f6e3'
should be 应该
`http://api.openweathermap.org/data/2.5/weather?zip=${this.state.query},us&appid=748f643131acee33c207bee1a969f6e3`
info on template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals 有关模板文字的信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Template_literals
Template literals are enclosed by the back-tick (
模板文字由反引号(
) (grave accent) character instead of double or single quotes.
)(重音符号)字符,而不是双引号或单引号。 Template literals can contain place holders.
模板文字可以包含占位符。 These are indicated by the Dollar sign and curly braces (${expression}).
这些由美元符号和大括号($ {expression})表示。 The expressions in the place holders and the text between them get passed to a function.
占位符中的表达式及其之间的文本将传递给函数。 The default function just concatenates the parts into a single string.
默认功能只是将各个部分串联为一个字符串。 If there is an expression preceding the template literal (tag here), the template string is called "tagged template literal".
如果在模板文字之前有一个表达式(在此处标记),则模板字符串称为“标记的模板文字”。 In that case, the tag expression (usually a function) gets called with the processed template literal, which you can then manipulate before outputting.
在这种情况下,将使用已处理的模板文字来调用标签表达式(通常是一个函数),然后可以在输出之前对其进行操作。 To escape a back-tick in a template literal, put a backslash \\ before the back-tick.
要在模板文字中避免反引号,请将反斜线\\放在反引号之前。
working pen http://codepen.io/finalfreq/pen/MpONrW 工作笔http://codepen.io/finalfreq/pen/MpONrW
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.