그럴 수 없는, 또는 권장됨 이해할 수 있는 모델을 사용할 수 있는 it 및 어레이에서는 감지에서 완료하십시오 '지도'. # 39 의 좋아요, 하지만 that& 갖고 있지 않은 경우 어레이에서는 어때? 그 후 여러 요소를 가지고 있는 것과 있는 숫자 값을 특정 렌더링하려는 어떻게 해야 합니까?
Javascript:
for (var i = 0; i < level; i++) {
$el.append('<span class="indent"></span>');
}
$el.append('Some text value');
레이스트리아스
render: function() {
var tmp = [];
for (var i = 0; i < this.props.level; i++) {
tmp.push(i);
}
var indents = tmp.map(function (i) {
return (
<span className='indent'></span>
);
});
return (
...
{indents}
"Some text value"
...
);
}
실로 이 can& # 39 만 t, 또는 이를 최고의 방법은? 내가 뭘 놓친거야?
한 요소에 반환되기를 렌더링합니다 방법을 가지고 있는 것은 아닙니다. 어레이에서는 귀의하지않는다고 수도 있습니다.
var indents = [];
for (var i = 0; i < this.props.level; i++) {
indents.push(<span className='indent' key={i}></span>);
}
return indents;
또는
return this.props.level.map((item, index) => (
<span className="indent" key={index}>
{index}
</span>
));
[ 여기에 대해 설명하는 문서를 이스스 하였더뇨 ] [1]
한 루프 대신 사용할 수 있습니다.
var indents = [];
for (var i = 0; i < this.props.level; i++) {
indents.push(<span className='indent' key={i}></span>);
}
return (
<div>
{indents}
"Some text value"
</div>
);
2 와 [.map ] 돌림무늬 es6 사용할 수도 있습니다.
return (
<div>
{this.props.level.map((item, index) => (
<span className='indent' key={index} />
))}
"Some text value"
</div>
);
또한 반환 값 (컨테이너입니다 마무리 합니다. 위의 예에서 div 사용
Here 라고 같은 문서
[1]: https://reactjs.org/docs/jsx-in-depth.html # 이스스 하였더뇨
다음은 일부 기능 있는 예 더 ES6 기능:
'use strict';
const React = require('react');
function renderArticles(articles) {
if (articles.length > 0) {
return articles.map((article, index) => (
<Article key={index} article={article} />
));
}
else return [];
}
const Article = ({article}) => {
return (
<article key={article.id}>
<a href={article.link}>{article.title}</a>
<p>{article.description}</p>
</article>
);
};
const Articles = React.createClass({
render() {
const articles = renderArticles(this.props.articles);
return (
<section>
{ articles }
</section>
);
}
});
module.exports = Articles;
// chars = {a:true, b:false, ..., z:false}
render() {
return (
<div>
{chars && Object.keys(chars).map(function(char, idx) {
return <span key={idx}>{char}</span>;
}.bind(this))}
"Some text value"
</div>
);
}
'아리스프롬 ()' 는 이트레이블 변환할지 객체에는 배열 및 선택적 매핑해야 함수은. '속성' 트렌크스 다음과 같이 있는 객체를 생성할 수 있습니다
return Array.from({length: this.props.level}, (item, index) =>
<span className="indent" key={index}></span>
);
내 생각에 이것은 js 반응할 수 있는 가장 쉬운 방법은 반복
<ul>
{yourarray.map((item)=><li>{item}</li>)}
</ul>