Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Syntax Error : Expected corresponding JSX closing tag for <img>

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>
like image 546
Taras Yaremkiv Avatar asked Nov 30 '16 18:11

Taras Yaremkiv


People also ask

Does IMG have a closing tag in JSX?

It is a void element, meaning that it has no content and does not have a closing tag.

How do I fix JSX expressions must have one parent element?

The React. js error "JSX expressions must have one parent element" occurs when a component returns multiple elements. To solve the error, wrap the elements in a parent div element or use a fragment, e.g. <><h2>One</h2><h2>Two</h2></> .

How do you add BR tag in React?

To add a br tag in React between two strings, we can use the white-space CSS property. const Comp = () => { const message = `No results. \n Please try another search term. `; return <div className="new-line">{message}</div>; };

Why do we use JSX?

JSX allows us to write HTML elements in JavaScript and place them in the DOM without any createElement() and/or appendChild() methods. JSX converts HTML tags into react elements. You are not required to use JSX, but JSX makes it easier to write React applications.


2 Answers

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.

like image 167
TimoStaudinger Avatar answered Nov 02 '22 00:11

TimoStaudinger


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.

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

or

<img src = {this.props.user.img} alt="logo" />
like image 25
Andrew Allison Avatar answered Nov 01 '22 23:11

Andrew Allison