当前位置: 首页 > 面试题库 >

如何在地图内访问正确的“ this”:ReactJS

李鸿
2023-03-14
问题内容

例如,我有一个带有两种绑定方法的react组件:

import React from 'react';

class Comments extends React.Component {
    constructor(props) {
        super(props);
        this.handleSubmit = this.handleSubmit.bind(this);
        this.handleRemoveComment = this.handleRemoveComment.bind(this);
    }

    handleSubmit(e) {
        .....
    }

    handleRemoveComment(e) {
        //this.props.removeComment(null, this.props.params, i);
    }

    renderComment(comment, i) {
        return(
            <div className="comment" key={i}>
                  .....
                  <button 
                       onClick={this.handleRemoveComment}
                       className="remove-comment">
                       &times;
                  </button>
            </div>
        )
    }

    render() {
        return(
            <div className="comments">

                {this.props.postComments.map(this.renderComment)}

                .....
            </div>
        )
    }
}

export default Comments;

在上面的代码中,我有两种绑定方法:一种是handleSubmit和一种是handleRemoveCommenthandleSubmit功能有效,但handleRemoveComment无效。运行时,它返回错误:

TypeError:无法读取未定义的属性“ handleRemoveComment”


问题答案:

问题在于这条线:

{this.props.postComments.map( this.renderComment )}

因为您忘记了绑定renderComment,映射回调方法,所以thisinside renderComment方法将不会引用类上下文。

使用这些解决方案中的 任何一种 ,它将起作用。

1-constructor以下位置使用此行:

this.renderComment = this.renderComment.bind(this) ;

2- 通过thismap喜欢:

{this.props.postComments.map(this.renderComment, this)}

3- 将Arrow函数与renderComment方法配合使用,如下所示:

renderComment = (comment, i) => {
    .....

或在renderComment函数内部使用地图(我以前更喜欢这种方式),如下所示:

renderComment() {
    return this.props.postComments.map((comment, i) => {
        return(
            <div className="comment" key={i}>
                <p>
                    <strong>{comment.user}</strong>
                    {comment.text}
                    <button 
                        onClick={this.handleRemoveComment}
                        className="remove-comment">
                        &times;
                    </button>
                </p>
            </div>
        )
    })
}

并从中调用此方法render,在这种情况下,renderComment不需要绑定:

{this.renderComment()}


 类似资料:
  • 问题内容: 我有一个注册事件处理程序的构造函数: 但是,我无法在回调内部访问已创建对象的属性。看起来好像不引用创建的对象,而是引用另一个对象。 我也尝试使用对象方法而不是匿名函数: 但是也有同样的问题 如何访问正确的对象? 问题答案: 你应该知道什么 (又名“上下文”)是每个功能内的特殊关键字和它的值仅取决于 如何 调用函数,而不是如何/何时/何它被定义。它不受其他变量之类的词法作用域的影响(箭头

  • 问题内容: 是否可以从Java内部类中获取对它的引用? 即 问题答案: 您可以像这样访问外部类的实例:

  • 我正在尝试使用新的@react Google maps/api库访问Web应用程序中安装的Google Map的getBounds方法,该库取代了tomchentw不支持的react Google maps库。 与旧的react GoogleMaps不同,这个新库似乎无法访问此处定义的GoogleMap对象。根据您尝试访问Google Map对象的方式,您将获得以下两种情况之一: 1)简单地将re

  • 对于类中的方法,文档是这样说的: 取消此对话框,将其从屏幕上移除。可以从任何线程安全地调用此方法。请注意,当对话框被取消时,不应重写此方法来进行清理,而应在中实现此方法。 在我的代码中,我所做的就是调用来删除它。但是我没有做其他任何事情,甚至没有使用。因此,我问的是如何正确地删除以避免任何内存泄漏等。

  • 问题内容: 我尝试遍历一个映射,将其作为指向函数的指针传递,但是我找不到访问元素的方法。这是代码: 此示例中的第4行返回以下编译错误: 我尝试了方括号,但没有效果。如果我带走所有引用运算符(*&),则可以正常编译。 我该怎么写? 问题答案: 您不需要在地图上使用指针。 映射类型是引用类型,例如指针或切片 如果需要更改,可以使用一个指针:

  • 问题内容: 我无法连接到以nginx入口运行的应用程序(Docker桌面赢10)。 nginx- ingress控制器容器正在运行,应用程序运行良好,并且我已经创建了一个入口。但是,当我尝试连接到本地主机上的应用程序时,出现“连接被拒绝”的提示。 我在日志中看到此错误: 我认为端口443已由其他应用程序使用,可能是zscaler安全性或skype。摘录自: 我不知道如何使入口工作。请帮忙! 我的入