本文介绍了React语法错误:< img>预期与之对应的JSX结束标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是React的初学者,我尝试通过FreeCodeCamp做一个"Camper Leader Board"项目.在StackOverflow代码段中,它抛出了我:
`"message":"SyntaxError:内联Babel脚本:预期与之对应的JSX结束标记请帮助我找出问题所在.这是代码:
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 forPlease 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>
推荐答案
您需要使用/>
结束符来关闭img
标记:
You need to close your img
tag with a closing />
:
<img src={this.props.user.img} alt="logo" />
在正确关闭标签时,JSX不如HTML宽容.
JSX is not as lenient as HTML when it comes to properly closing tags.
这篇关于React语法错误:< img>预期与之对应的JSX结束标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!