“这个"在地图函数 Reactjs 中未定义 [英] "this" is undefined inside map function Reactjs

查看:35
本文介绍了“这个"在地图函数 Reactjs 中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Reactjs,编写菜单组件.

"使用严格";var React = require("react");var Menus = React.createClass({item_url:功能(项目,类别,文章){console.log('afdasfasfasdfasdf');var url='XXX';如果(项目.类型== 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: 文章[item.articleId].slug, id: item.articleId});} 别的 {url = item.url;}返回网址;},渲染:函数(){//console.log(this.props.menus);//正确返回var menuElements = this.props.menus.map(function (item1) {//返回错误:'cannot read property 'props' of undefined '返回 (<div><li><a href={this.item_url(item1, this.props.categories, this.props.articles )}>{item1.name}//上面同样的故障<i class="glyphicon glyphicon-chevron-right pull-right"></i></a><div class="子菜单"><div>{item1._children.map(函数(item2){返回 (<div><h4><a href={this.item_url(item2, this.props.categories, this.props.articles)}>{ item2.name }</a><ul>{item2._children.map(函数(item3){返回 (<div><li><a href={this.item_url(item3, this.props.categories, this.props.articles) }>{ item3.name }</a></li>

);})}

);})}

);});返回 (<div class="菜单"><ul class="nav nav-tabs nav-stacked">{菜单元素}

);}});

每当我在 map 函数中使用 'this' 时,它都是未定义的,但在它之外就没有问题.

错误:

<块引用>

无法读取未定义的属性‘props’"

谁来帮帮我!:((

解决方案

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

someList.map(function(item) {...}, 这个)

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

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

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

"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.

The error:

"Cannot read property 'props' of undefined"

Anybody help me ! :((

解决方案

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

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

Alternatively, you can use an ES6 arrow function to automatically preserve the current this context:

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

这篇关于“这个"在地图函数 Reactjs 中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆