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>
It is a void element, meaning that it has no content and does not have a closing tag.
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></> .
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>; };
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.
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.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With