首页 > 解决方案 > Unexpected token, expected "," in JSX

问题描述

I am currently a student working on a react app, and keep getting returned syntax errors for unexpected tokens in my onSubmit() function. It wants commas instead of closing curly brackets, and I do not understand why. I add commas, swap out curly brackets for commas, then do it all over again to no avail. Any ideas where I might be making a mistake?

import React from 'react';
import axios from 'axios';
import Info from './Info';


export default class Search extends React.Component{
  state = {
    ingredientName: '',
  };

  change = e => {
    this.setState({
      [e.target.name] : e.target.value,
    });
  };

  onSubmit() {
    e.preventDefault();
    const userInput = JSON.stringify(this.state);
      axios.get(`https://www.recipepuppy.com/api/?i=${this.state}`).then((res) => {
        console.log(res.data),
      }
  };


};


  render(){
    return (
      <div>
      <form>
      <input name="ingredientName" placeholder="chicken, rice, tomatoes, etc" value={this.state.ingredientName} onChange={e => this.change(e)}/>
      <button onClick={e => this.onSubmit(e)}>Submit</button>
      </form>
      </div>
    )
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

标签: javascriptreactjssyntax-errorjsxcomma

解决方案


Update this line

console.log(res.data),

to be

console.log(res.data);

推荐阅读