[英]jsx render error with loop: elements not defined
I am trying to loop through obj in json format I get from hitting the api.我正在尝试以 json 格式遍历 obj,我从击中 api 得到。 However, when I try to render it, I kept running into the error that the element is not defined.但是,当我尝试渲染它时,我一直遇到未定义元素的错误。
This is my comment template return to html:这是我返回 html 的评论模板:
return (
<div>
this.state.comments.map((comment)=> (
{comment.owner}
{comment.text}
)
</div>
);
}
This is what the json file looks like:这是 json 文件的样子:
{
"comments": [
{
"owner": "user1",
"owner_show_url": "/u/user1/",
"postid": 1,
"text": "text1"
},
{
"owner": "user2",
"owner_show_url": "/u/user2/",
"postid": 1,
"text": "text2"
}
],
"url": "/api/v1/p/1/comments/"
}
this is the error i copied from the developer tool:这是我从开发人员工具中复制的错误:
bundle.js:1490 Uncaught ReferenceError: comment is not defined
at Comments.render (bundle.js:1490)
at finishClassComponent (bundle.js:7852)
at updateClassComponent (bundle.js:7849)
at beginWork (bundle.js:7945)
at performUnitOfWork (bundle.js:8265)
at workLoop (bundle.js:8289)
at HTMLUnknownElement.callCallback (bundle.js:6267)
at Object.invokeGuardedCallbackDev (bundle.js:6283)
at invokeGuardedCallback (bundle.js:6222)
at performWork (bundle.js:8325)
Could be that you need to wrap javascript within jsx using {}:可能是您需要使用 {} 将 javascript 包装在 jsx 中:
{this.state.comments.map()}
you should also contain jsx within a parent component or a html tag (such I did here with the li as is standard when list building)您还应该在父组件或 html 标记中包含 jsx(我在这里使用 li 作为列表构建时的标准)
EDIT: I initialized comments state with 2 objects however your App should initialize state simply with an empty array:编辑:我用 2 个对象初始化了评论 state 但是你的应用程序应该用一个空数组初始化 state :
state={
comments:[]
}
this should work (assuming your setState is being done properly):这应该有效(假设您的 setState 正在正确完成):
class SomeComponent extends React.Component{ state={ comments:[ {owner:'owner1', text:'comment2'}, {owner:'owner2', text:'comment2'} ] } render(){ return ( <ul> {this.state.comments.map((comment)=> { return <li> {comment.owner} - {comment.text} </li> })} </ul> ); } } ReactDOM.render( <SomeComponent />, document.getElementById("react") );
<div id='react'></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Where did you declare your state?你在哪里声明你的 state? Did you add the constructor to the code?您是否将构造函数添加到代码中?
this.state = {comments: new Comment()};
I also noticed that you missed some indentation.我还注意到你错过了一些缩进。 You need curly braces around this.state...
and you also missed parentheses.你需要在this.state...
而且你也错过了括号。
Check if you've declared state.检查您是否已声明 state。
constructor() {
this.state = { comments: [] }
}
Also, it would help to initialize it or at least assume an empty array if there are no values set to it yet此外,如果还没有设置任何值,它会有助于初始化它或至少假设一个空数组
(this.state.comments || []).map(..)
And don't forget to wrap it with curly braces when inside the render
function并且不要忘记在render
function 内时用花括号将其包裹起来
{(this.state.comments || []).map(..)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.