朋友。我收到一个语法错误,指向渲染上方的右大括号。它说它期待逗号,但我不明白为什么。所有花括号都有开括号和右括号。我错过了什么?
import React, {Component} from 'react';
import axios from 'axios';
class List extends Component {
constructor(props){
super(props)
this.state = {
sports: []
}
}
componentWillMount(){
axios.get('my url is in here')
.then((response) => {
this.setState({
sports: response
})
}
}
render(){
return(
<div>
<p>{this.state.sports} </p>
</div>
)
}
}
export default List;
您缺少右括号:
componentWillMount(){
axios.get('my url is in here')
.then((response) => {
this.setState({
sports: response
})
}) // <-- this )
}
}
你需要关闭.then()
如下:
componentWillMount() {
axios.get('my url is in here').then(response => {
this.setState({
sports: response,
});
}); //<--- here, a ) is needed
}
import React, {Component} from 'react';
import axios from 'axios';
class List extends Component {
constructor(props){
super(props)
this.state = {
sports: []
}
}
componentWillMount(){
axios.get('my url is in here')
.then((response) => {
this.setState({
sports: response
})
})
}
render(){
return(
<div>
<p>{this.state.sports} </p>
</div>
)
}
}
export default List;
your updated code..
you just miss the closing bracket in componentWillMount() method.
componentWillMount(){
axios.get('my url is in here')
.then((response) => {
this.setState({
sports: response
})enter code here
}) // <-- this )
}
}