简体   繁体   English

React Component问题(预期为“;”,但发现为“,”)

[英]React Component problem (';' expected, but found ',')

This is driving me crazy. 这真让我抓狂。 I just started playing around with react trying to learn, but i can't even make a simple API call because of a extremely weird error.... 我刚刚开始尝试学习的反应,但是由于一个非常奇怪的错误,我什至无法进行简单的API调用。

This is my code -> 这是我的代码->

import React, {Component} from 'react';

class ApiPosts extends Component {
    constructor() {
        super();
        this.state = {
            blogPosts: [],
        };
    }
}

componentDidMount(){

    fetch('http://localhost:53595/blog/posts')
    .then(results => {
        return results.json();
    }).then(data => {
        let blogPosts = data.results.map((post) => {
            return(
                <div key={post.results}>
                    <div>{post.body}</div>
                </div>
            )
        });
        this.setState({blogPosts: blogPosts});
        console.log("state", this.state.blogPosts);
    })
}

render(){
    return (
        <div className="container2">
            <div className="container1">
                {this.state.blogPosts}
            </div>
        </div>
    )
}

Lines 12 and 30 ("componentDidMount(){ & render(){") are throwing me an error showing that i haven't closed them with ';'. 第12行和第30行(“ componentDidMount(){和render(){”)向我抛出一个错误,表明我没有用';'将其关闭。

The error shows up in visual studio code & fails to build my app with the following error -> 该错误显示在Visual Studio代码中,并且无法构建我的应用,并出现以下错误->

/react-website/src/ApiPosts.js: Unexpected token, expected ; (12:19)

I literally tried to close EVERYTHING in that file just to see where the error is coming from, but no luck. 我确实试图关闭该文件中的所有内容,只是为了查看错误的出处,但没有运气。

Any idea? 任何想法?

Move componentDidMount and render function within your component class. 在您的组件类中移动componentDidMount和render函数。 They are outside of the class in given code snippet. 在给定的代码片段中,它们不在类中。

import React, {Component} from 'react';

class ApiPosts extends Component {
    constructor() {
        super();
        this.state = {
            blogPosts: [],
        };
    }
    componentDidMount() {}
    render() {}
}

You have componentDidMount and render defined outside of the component itself. 您具有componentDidMount并在组件本身之外定义了render

It should look like this: 它看起来应该像这样:

import React, {Component} from 'react';

class ApiPosts extends Component {
    constructor() {
        super();
        this.state = {
            blogPosts: [],
        };
    }

    componentDidMount() {
       fetch('http://localhost:53595/blog/posts')
        .then(results => {
            return results.json();
        }).then(data => {
            let blogPosts = data.results.map((post) => {
                return(
                    <div key={post.results}>
                        <div>{post.body}</div>
                    </div>
                )
            });
            this.setState({blogPosts: blogPosts});
            console.log("state", this.state.blogPosts);
        })
    }

    render() {
        return (
            <div className="container2">
                <div className="container1">
                    {this.state.blogPosts}
                </div>
            </div>
        )
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM