Javascript 如何调用循环中的组件

Javascript 如何调用循环中的组件,javascript,reactjs,foreach,coffeescript,Javascript,Reactjs,Foreach,Coffeescript,我想在CategoryBox组件中的forEach循环中调用CategoryProduct组件 我的输入: - productos = [ { header: 'car', src: 'picture.jpg', description: 'the car' } ] 类别产品组件: @CategoryProduct = React.createClass render: -> div className: 'CategoryProduct' div className

我想在CategoryBox组件中的forEach循环中调用CategoryProduct组件

我的输入

- productos =  [ { header: 'car', src: 'picture.jpg', description: 'the car' } ]
类别产品组件:

@CategoryProduct = React.createClass
render: ->
 div
  className: 'CategoryProduct'
  div
    className: 'header'
    @props.header
  div
    className: 'picture'
    @props.src
  div
    className: 'description'
    @props.description
然后我尝试调用CategoryBox组件中的CategoryProduct组件:

@CategoryBox = React.createClass
 render: ->
  div
   className: 'CategoryBox'
   @props.productos.forEach (prod) ->
     React.createElement CategoryProduct, { header: prod.header, src: prod.src, description: prod.description }

但是,的输出什么都不是。我在循环中做了一个控制台日志,可以顺利进入,但是它没有进入类别产品组件
数组。prototype.forEach()
不会返回。您必须改用
Array.prototype.map()

所以你的代码应该是这样的

@CategoryBox = React.createClass
 render: ->
  div
   className: 'CategoryBox'
   @props.productos.map(prod) ->
     React.createElement CategoryProduct, { header: prod.header, src: prod.src, description: prod.description }

Array.prototype.forEach()不返回。您必须改用
Array.prototype.map()

所以你的代码应该是这样的

@CategoryBox = React.createClass
 render: ->
  div
   className: 'CategoryBox'
   @props.productos.map(prod) ->
     React.createElement CategoryProduct, { header: prod.header, src: prod.src, description: prod.description }

你应该看看咖啡脚本。它们比JS的要好得多,由于coffeescripts隐式返回,循环将返回一个数组,其中包含循环中最后一行的值

例如:

a = [1,2,3,4,5,6]
b = for item in a 
  z = item * 2
  z + 1
然后
b
将是
[3,5,7,9,11,13]

所以你的代码应该是:

@CategoryBox = React.createClass
  render: ->
    div
      className: 'CategoryBox'
      for prod in @props.productos
       React.createElement CategoryProduct, prod

你应该看看咖啡脚本。它们比JS的要好得多,由于coffeescripts隐式返回,循环将返回一个数组,其中包含循环中最后一行的值

例如:

a = [1,2,3,4,5,6]
b = for item in a 
  z = item * 2
  z + 1
然后
b
将是
[3,5,7,9,11,13]

所以你的代码应该是:

@CategoryBox = React.createClass
  render: ->
    div
      className: 'CategoryBox'
      for prod in @props.productos
       React.createElement CategoryProduct, prod