简体   繁体   English

顺风顺水:遍历ActiveRecord数组

[英]React on rails:Iterating through an array of activerecord

I am new to React with Rails and use of react-rails gem. 我是使用Rails进行React和使用react-rails gem的新手。

I am trying to loop through all lifts and show the liftname through Lift component. 我试图遍历所有电梯,并通过“电梯”组件显示电梯名称。

The controller action index provides: 控制器操作索引提供:

class LiftsController < ApplicationController

    def index
        @lifts = Lift.all
    end
end

Under assets/javascripts/components I have a lift.js.jsx component. assets/javascripts/components我有一个lift.js.jsx组件。

class Lifts extends React.Component {
  render() {
    const arr = {this.props.data}.map((x) => <li>Hello {x.liftname}!</li>);
    return (<ul>{arr}</ul>);    
  }
}

In the view page, I am passing the data as shown below: 在视图页面中,我正在传递数据,如下所示:

<%= react_component('Lifts', data: @lifts) %>

But, I am receiving the error: 但是,我收到错误:

SyntaxError: unknown: Unexpected token (3:20)
  1 | class Lifts extends React.Component {
  2 |   render() {
> 3 |       const arr = {this.props.data}.map((x) => <li>Hello {x.liftname}!</li>);
    |                     ^
  4 |       return (<ul>{arr}</ul>);    
  5 |   }
  6 | }

My questions are: 我的问题是:

  1. Where I am going wrong 我要去哪里了
  2. Is there any debugger for react which gives more info apart from unexpected token ? 是否有任何针对React的调试器,可提供除意外令牌之外的更多信息?

Probably this line should be without curly braces in the first case, like this: 在第一种情况下,该行可能应该没有大括号,例如:

const arr = this.props.data.map((x) => <li>Hello {x.liftname}!</li>);

And you also have to define the key prop for every li item otherwise React'll drop a warning. 而且您还必须为每个li项目定义key道具,否则React会发出警告。

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

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