简体   繁体   English

JSX for ... in循环

[英]JSX for…in loop

Given this object: 鉴于此对象:

lst socials = {
  foo: 'http://foo'
}

I want to loop through it in JSX. 我想在JSX中循环它。 This works: 这有效:

let socialLinks = []
let socialBar
for (let social in socials) {
  socialLinks.push(<li>
                     <a alt={social} href={socials[social]}>{ social }</a>
                   </li>)
}
if (socialLinks) {
  socialBar = <div className='align-bottom text-center'>
                <ul className='list-inline social-list mb24'>
                  {socialLinks}
                </ul>
              </div>
}

But this doesn't (social undefined): 但这不是(社会未定义):

let socialBar
if (socials) {
  socialBar = <div className='align-bottom text-center'>
                <ul className='list-inline social-list mb24'>
                  for(let social in socials)
                  {<li>
                     <a alt={social} href={socials[social]}>{ social }</a> // social is undefined
                   </li>}
                </ul>
              </div>
}

What is the reason social is undefined in the 2nd example? 在第二个例子中social未定义的原因是什么? I assume there is a scoping issue with the inner brackets but I have not been successful fixing it. 我假设内部括号存在范围问题,但我没有成功修复它。

I can do do a forEach with object keys and do as in this post but that's not much different than my working example. 我可以用对象键做一个forEach ,并像在这篇文章中那样做,但这与我的工作示例没什么不同。

To be clear - I have it working, I simply wish to be clearer on the scoping problem (or syntax error if so) in my 2nd example. 要清楚 - 我有它工作,我只是希望在我的第二个例子中更清楚地确定范围问题(或语法错误,如果是这样)。

JSX is just sugar that gets transpiled to a bunch of function calls of React.createElement , which you can find the docs for here: https://facebook.github.io/react/docs/top-level-api.html#react.createelement JSX只是糖被转化为React.createElement的一堆函数调用,您可以在这里找到文档: httpsReact.createElement .createelement

ReactElement createElement(
  string/ReactClass type,
  [object props],
  [children ...]
)

Basically your JSX goes from 基本上你的JSX来自

<div style="color: white;">
  <div></div>
</div>

to

React.createElement('div', { style: { color: 'white' } }, [
  React.createElement('div', {}, [])
])

For the same reason you can't pass a loop to a parameter in a function, you can't put a loop into JSX. 出于同样的原因,你不能将循环传递给函数中的参数,你不能把循环放到JSX中。 It would end up looking like 它最终看起来像

React.createElement('div', { style: { color: 'white' } }, [
  React.createElement('div', {}, for (;;) <div></div>)
])

which doesn't make sense at all because you can't pass a for loop as a param. 这根本没有意义,因为你不能将for循环作为参数传递。 On the other hand, a map call returns an array, which is the correct type for the third parameter of React.createElement. 另一方面,map调用返回一个数组,该数组是React.createElement的第三个参数的正确类型。

React is still a virtual dom library at the end of the day, but JSX just makes it more familiar to write. React在一天结束时仍然是一个虚拟dom库,但JSX只是让它更熟悉。 hyperscript is another good example of a vdom library, but where JSX is not standard. hyperscript是vdom库的另一个很好的例子,但JSX不是标准的。 Their example on their README is similar to what React would look like without JSX: 他们的自述文件中的示例类似于没有JSX时React的样子:

var h = require('hyperscript')
h('div#page',
  h('div#header',
    h('h1.classy', 'h', { style: {'background-color': '#22f'} })),
  h('div#menu', { style: {'background-color': '#2f2'} },
    h('ul',
      h('li', 'one'),
      h('li', 'two'),
      h('li', 'three'))),
    h('h2', 'content title',  { style: {'background-color': '#f22'} }),
    h('p',
      "so it's just like a templating engine,\n",
      "but easy to use inline with javascript\n"),
    h('p',
      "the intension is for this to be used to create\n",
      "reusable, interactive html widgets. "))

In your JSX you can't have a for loop. 在你的JSX中你不能有一个for循环。 So even if you have {} around your for loop it doesn't work. 因此,即使你的for循环周围有{} ,它也不起作用。 Instead use a map as shown in the below code. 而是使用下面代码中显示的地图。 Assuming your data socials is an array and not just an object. 假设您的数据socials is an array而不仅仅是一个对象。

If socials is an object you need to use Object.keys(socials).map(function(key)){} 如果社交是一个对象,你需要使用Object.keys(socials).map(function(key)){}

 class App extends React.Component { render() { let socialBar = null; let socials = [{ foo: 'http://foo' }] if (socials) { socialBar = <div className='align-bottom text-center'> <ul className='list-inline social-list mb24'> {socials.map(function(social, index) { return <li key={index}> <a alt={index} href={social.foo}>{ social.foo }</a> </li> }) } </ul> </div> } return ( <div>{socialBar}</div> ) } } ReactDOM.render(<App/>, document.getElementById('app')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.js"></script> <div id="app"></div> 

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

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