我的意外令牌错误发生在哪里?

问题描述 投票:1回答:3

朋友。我收到一个语法错误,指向渲染上方的右大括号。它说它期待逗号,但我不明白为什么。所有花括号都有开括号和右括号。我错过了什么?

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;
javascript reactjs
3个回答
2
投票

您缺少右括号:

componentWillMount(){
  axios.get('my url is in here')
    .then((response) => {
      this.setState({
        sports: response
      })
    }) // <-- this )
  }
}

2
投票

你需要关闭.then()如下:

  componentWillMount() {
    axios.get('my url is in here').then(response => {
      this.setState({
        sports: response,
      });
    }); //<--- here, a ) is needed
  }

1
投票
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 )
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.