简体   繁体   English

如何在循环内调用组件

[英]How can I call a component inside a loop

I want to call a CategoryProduct component inside a forEach loop in the CategoryBox Component. 我想在CategoryBox组件的forEach循环内调用CategoryProduct组件。

My input : 我的输入

- productos =  [ { header: 'car', src: 'picture.jpg', description: 'the car' } ]

CategoryProduct component: 产品类别:

@CategoryProduct = React.createClass
render: ->
 div
  className: 'CategoryProduct'
  div
    className: 'header'
    @props.header
  div
    className: 'picture'
    @props.src
  div
    className: 'description'
    @props.description

Then I try to call the CategoryProduct component in the CategoryBox component: 然后,我尝试在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 }

But the output is nothing. 但是输出是什么。 I did a console log inside the loop and is getting in without problem but it doesn't go inside the CategoryProduct component 我在循环内做了一个控制台日志,没有问题,但是没有进入CategoryProduct组件

Array.prototype.forEach() doesn't return anything . Array.prototype.forEach()不返回任何内容 You have to use Array.prototype.map() instead. 您必须改为使用Array.prototype.map()

So your code should look like this 所以你的代码应该像这样

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

You should look into coffeescript's loops and comprehensions . 您应该研究coffeescript的循环和理解力 They are much nicer than the JS ones and due coffeescripts implicit return, the loop will return an array containing the values on the last line inside the loop. 它们比JS更好,并且由于coffeescripts隐式返回,因此循环将返回包含循环内最后一行值的数组。

For example: 例如:

a = [1,2,3,4,5,6]
b = for item in a 
  z = item * 2
  z + 1

Then b would be [3, 5, 7, 9, 11, 13] 那么b将是[3, 5, 7, 9, 11, 13]

So your code would read: 因此,您的代码将显示为:

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

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

相关问题 如何在子组件内部调用父组件的功能? - How can I call the function of a parent component inside of a child component? 如何在reactjs中调用无状态功能组件内部的函数 - How can I call a function inside of stateless functional component in reactjs 我可以在另一个组件中调用 div 吗? - Can I call a div inside another component? 如何在循环内调用异步 function? - How can I call async function inside a loop? 如何在循环内循环并通过 AJAX 调用对 JSON object 的结果进行排序? - How can I loop inside a loop and sort results from a JSON object via AJAX call? 如何调用 function 在 React Native 的自定义组件中设置外部变量? - How can I call a function that set sets an outside variable inside a custom component in React Native? 我可以通过单击按钮调用组件吗?我可以直接在 onclick 事件值中传递组件吗? - can i call a component by clicking a button and can i directly pass a component inside the onclick event value? 如何在另一个组件中获取指令/组件实例? - How can I get a directive/component instance inside another component? 我如何使用脚本调用来调用 Web 组件 - How can i call a web-component with a script call 如何在循环中调用JavaScript函数 - How can I call JavaScript function in a loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM