반응형
지도 함수 내에서 "this"가 정의되지 않았습니다. Reactjs
Reactjs와 함께 메뉴 컴포넌트를 만들고 있습니다.
"use strict";
var React = require("react");
var Menus = React.createClass({
item_url: function (item,categories,articles) {
console.log('afdasfasfasdfasdf');
var url='XXX';
if (item.type == 1) {
url = item.categoryId == null ? 'javascript:void(0)' : path('buex_portal_browse_category', {slug: categories[item.categoryId].slug});
} else if (item.type == 2) {
url = item.articleId == null ? 'javascript:void(0)' : path('buex_portal_view_article', {slug: articles[item.articleId].slug, id: item.articleId});
} else {
url = item.url;
}
return url;
},
render: function () {
// console.log(this.props.menus); // return correctly
var menuElements = this.props.menus.map(function (item1) { // return fault : 'cannot read property 'props' of undefined '
return (
<div>
<li>
<a href={this.item_url(item1, this.props.categories, this.props.articles )}>{item1.name} // the same fault above
<i class="glyphicon glyphicon-chevron-right pull-right"></i>
</a>
<div class="sub-menu">
<div>
{item1._children.map(function (item2) {
return (
<div>
<h4>
<a href={this.item_url(item2, this.props.categories, this.props.articles)}>{ item2.name }</a>
</h4>
<ul>
{item2._children.map(function (item3) {
return (
<div><li><a href={this.item_url(item3, this.props.categories, this.props.articles) }>{ item3.name }</a></li></div>
);
})}
</ul>
</div>
);
})}
</div>
</div>
</li>
</div>
);
});
return (
<div class="menu">
<ul class="nav nav-tabs nav-stacked">
{menuElements}
</ul>
</div>
);
}
});
inside map 기능을 사용할 때는 항상 정의되어 있지 않지만, inside map 기능을 사용할 때는 문제가 없습니다.
오류:
"정의되지 않은 속성 'props'를 읽을 수 없습니다."
누구 좀 도와주세요! :(
Array.prototype.map()
두 번째 인수를 사용하여 무엇을 설정합니다.this
맵핑 기능에서 참조하기 때문에,this
현재 컨텍스트를 보존하기 위한 두 번째 인수로 지정합니다.
someList.map(function(item) {
...
}, this)
또는 ES6 화살표 기능을 사용하여 전류를 자동으로 보존할 수 있습니다.this
콘텍스트:
someList.map((item) => {
...
})
언급URL : https://stackoverflow.com/questions/30148827/this-is-undefined-inside-map-function-reactjs
반응형
'sourcecode' 카테고리의 다른 글
Google Angular 지도JS (0) | 2023.03.14 |
---|---|
각도로 ng-repeat을 사용하여 100개 중 6개에서 10개까지의 결과를 필터링합니다.JS (0) | 2023.03.14 |
MongoDB BSON 문서 크기 제한에 대해 (0) | 2023.03.14 |
React 구성 요소 디렉토리에서 index.js 파일은 어떻게 작동합니까? (0) | 2023.03.14 |
HikariCP 스프링 부트 로깅 (0) | 2023.03.09 |