React Syntax Error : Expected corresponding JSX cl

2020-03-02 05:12发布

问题:

I'm beginner in React and I try to do a "Camper Leader Board" project from FreeCodeCamp. In StackOverflow code snippet it throws me:
` "message": "SyntaxError: Inline Babel script: Expected corresponding JSX closing tag for Please help me to find out what is wrong. Here's the code:

"use strict";
class TableBox extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: []
        };
    }

    loadCampersFromServer() {
        fetch('https://fcctop100.herokuapp.com/api/fccusers/top/alltime')//alltime or recent
            .then(
                (response) => {
                    if (response.status !== 200) {
                        console.log('Looks like there was a problem. Status Code: ${response.status}');
                        return;
                    }
                    response.json().then((data) => {
                        console.log('getting data:', data);
                        this.setState({data: data});

                    })
                }
            )
            .catch(function (err) {
                console.log('Fetch Error :-S', err);
            });
    }

    componentDidMount() {
        this.loadCampersFromServer();
    }

    render() {
        return <CampersList />;
    }
}

class CampersList extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        console.log(this.state);
        let campersNodes = this.state.data.map((element, index) => {
            return (
                <Camper user={element} index={index} />

            );
        });
        return (
            <table>
                <tr>
                    <th>#</th>
                    <th>Camper Name</th>
                    <th>Points in past 30 days</th>
                    <th>All time points </th>
                </tr>
                {campersNodes}
            </table>
        )
    }
}


class Camper extends React.Component{
     constructor(props){
         super(props);
     }
     render(){
         <tr>
             <td>{this.props.index}</td>
             <td>
                 <img src = {this.props.user.img} alt="logo">
                 {this.props.user.userName}
             </td>
             <td>{this.props.user.recent}</td>
             <td>{this.props.user.alltime}</td>
         </tr>
     }
}

ReactDOM.render(<TableBox />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

回答1:

You need to close your img tag with a closing />:

<img src={this.props.user.img} alt="logo" />

JSX is not as lenient as HTML when it comes to properly closing tags.



回答2:

The message returned by the compiler is telling you there is no closing tag for the img element. JSX isn't the same as html.



回答3:

As the error explain itself you need to close the image tag in camper component.

<td>
    <img src = {this.props.user.img} alt="logo" />{this.props.user.userName}
</td >

This should work.