简体   繁体   English

REACT JS:未捕获的错误:解析错误:意外的令牌

[英]REACT JS : Uncaught Error: parse Error: Unexpected token

I am new to react (I am incorporating it into my ruby on rails project), here the component ive made: 我是新来的反应者(我将其合并到我的ruby on rails项目中),在这里,组件ive做了:

  <div id="articles"></div>
  <script type="text/jsx">



var Article=React.createClass({
    render: function(){
         return(
           <div>
             {this.props.data}.map(function(item){
               {item.name}  
             })
           </div>
         )
    }
});




var input = [{name: "hello", email: "hello@example.com"}]

React.render(<Article data={input} />, document.getElementById("articles"))

When i run this, this is the error i get: 当我运行它时,这是我得到的错误:

Uncaught Error: Parse Error: Line 7: Unexpected token .

{item.name}  

I have set a prop to be an array. 我已经将prop设置为数组。 I just want to output the name hash key inside the array - why am i getting this error, it seems to me that this should work ? 我只想在数组中输出名称哈希键-为什么我收到此错误,在我看来这应该起作用?

You're not returning anything inside your map-statement, try writing 您没有在地图声明中返回任何内容,请尝试编写

return {item.name};

However, I would suggest you try moving your map-function outside of the immediate render, its easier to read and less error-prone. 但是,我建议您尝试将地图功能移到即时渲染之外,它更易于阅读且不易出错。

 <div id="articles"></div>
  <script type="text/jsx">

var Article=React.createClass({
    render: function() {
         var rows = this.props.data.map(function(item) {
               //You could return any valid jsx here, like <span>{item.name}</span>
               return item.name;
         });
         return(
           <div>  
               {rows}
           </div>
         )
    }
});

var input = [{name: "hello", email: "hello@example.com"}
React.render(<Article data={input} />, document.getElementById("articles"))

In JSX you have two 'modes'. 在JSX中,您有两个“模式”。 JS mode (default) and JSX mode. JS模式(默认)和JSX模式。

You enter JSX mode with a tag, like <div . 您进入带有标记的JSX模式,例如<div In JSX mode only tags, attributes, and arbitrary text are allowed. 在JSX模式下,仅允许使用标签,属性和任意文本。 You are also allowed to go into a JS mode with {} . 您还可以通过{}进入JS模式。 This can happen any number of times. 这可以发生很多次。

function jsMode(){
    <jsx>
        {js(
            moreJs,
            <moreJsx>{evenMoreJs}</moreJsx>
        )}
    </jsx>;
}

So coming back to your code: 所以回到您的代码:

<div>
    {this.props.data}.map(function(item){
        {item.name}  
    })
</div>

Let's break this down into chunks 让我们将其分解为大块

// js mode
<div> // begin jsx
{ // begin js
    this.props.data // some js code
} // end js, back to jsx
.map(function(item) // this is just plain text visible to the user
{ // begin js
{item.name} // some invalid js, SyntaxError
} // end js
) // this is just plain text visible to the user
</div> // end jsx
// js mode

Because you want the .map to be interpreted as JS, and you were previously in JSX mode, it should also be in the {}s . 因为您希望将.map解释为JS,并且您以前处于JSX模式,所以它也应该位于{}s

<div>
    {this.props.data.map(function(item){
        {item.name}  
    })}
</div>

Also, inside the .map callback, you're still in JS mode, so you need to remove the {}s 另外,在.map回调中,您仍处于JS模式,因此需要删除{}s

<div>
    {this.props.data.map(function(item){
        item.name
    })}
</div>

And finally, you need to return the name from the .map callback. 最后,您需要从.map回调返回名称。

<div>
    {this.props.data.map(function(item){
        return item.name;
    })}
</div>

Other stuff 其他的东西

The code above will work, but probably not as expected. 上面的代码可以工作,但可能不符合预期。

If data was [{name: 'foo'}, {name: 'bar'}] , map will return ['foo', 'bar'] , and react will render it as: 如果数据是[{name: 'foo'}, {name: 'bar'}] ,则map将返回['foo', 'bar'] ,并且react会将其呈现为:

<span>foo</span><span>bar</span>

To the user this appears as "foobar". 对于用户,这显示为“ foobar”。 If you want it to appear as two separate words you could put a space after each name: 如果您希望它显示为两个单独的单词,则可以在每个名称后放置一个空格:

<div>
    {this.props.data.map(function(item){
        return item.name + " ";
    })}
</div>

Or return an element from .map and style it as you like. 或从.map返回一个元素并根据需要设置其样式。 For example, an unordered list of names. 例如,一个无序的名称列表。 Note that here we wrap item.name in {}s because we enter JSX mode. 请注意,由于进入JSX模式,因此此处将item.name包装在{}s

<ul>
    {this.props.data.map(function(item, i){
        return <li key={i}>{item.name}</li>;
    })}
</ul>

We also provide a key. 我们还提供了一把钥匙。 In this case it's the index of the item, which works as long as the list never changes, or only has items added/removed to/from the end of the array. 在这种情况下,它是项目的索引,只要列表永不更改,或者仅将项目添加到数组的末尾或从数组的末尾移出,该索引才起作用。

堆栈数据结构图

Image credit: wikipedia/commons 图片来源:Wikipedia / commons

If the array is reordered, or items are added/remove to/from the start or middle, we need a unique string identifier for each item. 如果对数组进行了重新排序,或者将项目添加到/从开始或中间添加/删除,则我们需要为每个项目使用唯一的字符串标识符。

If your data was [{name: "foo", id: "14019751"}, {name: "bar", id: "13409185"}] , then: 如果您的数据为[{name: "foo", id: "14019751"}, {name: "bar", id: "13409185"}] ,则:

<ul>
    {this.props.data.map(function(item, i){
        return <li key={item.id}>{item.name}</li>;
    })}
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM