Javascript “this”在地图函数 Reactjs 中未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30148827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:44:09  来源:igfitidea点击:

"this" is undefined inside map function Reactjs

javascriptreactjsthismap-function

提问by iamhuy

I'm working with Reactjs, writing a menu component.

我正在使用 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>
        );
    }
});

Whenever I use 'this' inside map function it is undefined, but outside it is no problem.

每当我在 map 函数内部使用 'this' 时,它都是未定义的,但在外部没有问题。

The error:

错误:

"Cannot read property 'props' of undefined"

“无法读取未定义的属性‘道具’”

Anybody help me ! :((

任何人都帮助我!:((

回答by Jonny Buchanan

Array.prototype.map()takes a second argument to set what thisrefers to in the mapping function, so pass thisas the second argument to preserve the current context:

Array.prototype.map()需要第二个参数来设置this映射函数中引用的内容,因此this作为第二个参数传递以保留当前上下文:

someList.map(function(item) {
  ...
}, this)

Alternatively, you can use an ES6 arrow functionto automatically preserve the current thiscontext:

或者,您可以使用 ES6箭头函数自动保留当前this上下文:

someList.map((item) => {
  ...
})