- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正在按照本教程制作一个简单的待办事项应用程序 (https://hackernoon.com/create-a-simple-todo-app-in-react-9bd29054566b)。
事情是我遇到了这个错误:
TypeError: _this is undefined
引用我在以下组件中使用onClick删除时:
import React, {Component} from 'react'
class TodoItems extends Component {
createTasks(item) {
return (
<li key={item.key} onClick={() => this.props.deleteItem(item.key)}>
{item.text}
</li>
)
}
render() {
const todoEntries = this.props.entries
const listItems = todoEntries.map(this.createTasks)
return <ul className="theList">{listItems}</ul>
}
}
export default TodoItems
我已经看到我可能必须在构造函数中使用 .bind(this) 但我不确定如何在此处应用它。
这是 App.js 的片段,其中定义了 deleteItem 并调用了 TodoItems:
deleteItem = key => {
const filteredItems = this.state.items.filter(item => {
return item.key !== key
})
this.setState({
items: filteredItems,
})
}
inputElement = React.createRef()
render() {
return (
<div className="App">
<TodoList
addItem={this.addItem}
inputElement={this.inputElement}
handleInput={this.handleInput}
currentItem={this.state.currentItem}
/>
<TodoItems entries={this.state.items} deleteItem={this.deleteItem}/>
</div> //App
)
}
在此先感谢您的帮助!
最佳答案
如果你不知道为什么...
The bodies of class declarations and class expressions are executed in strict mode, that is the constructor, static and prototype methods. Getter and setter functions are executed in strict mode.
在 React 的类组件中,当我们像这样将事件处理函数引用作为回调传递时
<button type="button" onClick={this.handleClick}>Click Me</button>
点我事件处理程序方法失去了其隐式绑定(bind)的上下文。当事件发生并调用处理程序时,this 值回退到默认绑定(bind)并设置为 undefined ,因为类声明和原型(prototype)方法在严格模式下运行。
当我们将事件处理程序的 this 绑定(bind)到构造函数中的组件实例时,我们可以将它作为回调传递,而不必担心它会丢失其上下文。
箭头函数不受此行为的影响,因为它们使用词法 this 绑定(bind),自动将它们绑定(bind)到它们定义的范围。
This is why we need to bind event handlers in Class Components in React
关于reactjs - 我如何在 React 中解决 "_this is undefined"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53627803/
我写了以下代码片段: export class Kurse extends React.Component { constructor(props) { super(props);
这个问题在这里已经有了答案: How to access the correct `this` inside a callback (13 个答案) 关闭 7 年前。 我一直在搜索,但找不到以 ES
我在使用 TypeScript 将 $(this) 替换为 $(_this) 并破坏代码时遇到问题,因为 test 指的是 窗口。 $(".class").click(() => { var
这是一个非常人为的例子,但假设我们在类函数的某处创建了一个变量 _this。 class Person { constructor (public name : string) {} chan
我尝试在 TypeScript 中为 String.Prototype 定义一些属性: Object.defineProperty(String.prototype, 'test', { va
我创建了一个Vue组件,调用imageUpload并将属性作为v模型传递 并且在imgeUpload组件内 我有这个代码 upload:(e)=>{ const files = e.tar
这两种说法有什么区别jquery 中的 $(this) 和 $(_this)。 最佳答案 $(this) 是上下文的当前元素选择器,但 $(_this) 是普通变量选择器。 例如: $('p').on
我正在尝试将最初用 JavaScript 编写的用于捕获视频的实用程序转换为 Angular。 我收到错误_this.video 未定义。原包在这里:https://developer.mozilla
您好,我正在尝试用 JavaScript 创建俄罗斯方 block 。我有一个带有名为 data 的变量的 map 对象。当我从对象调用函数时,“this”不再是对象,而是我认为的本地函数。我尝试在对
我收到以下错误消息:在使用 karma/ Jasmine 的Angular 6单元测试中,this.handler.handle不是函数。当我在项目文件夹中键入“ng test”命令时,在命令行中会出
我正在使用 Gatsby 构建一个网站,现在我正在创建一个 blog-post.js 文件,该文件通过来自 Markdown 文件的 graphql 数据进行查询并为每个文件创建页面。它还应包括页眉、
我有一个可以像这样简化的类: Captcha = function(el) { this.el = $(el); _this = this; captchas.push(thi
我正在尝试整体改进我的代码,尽管以下代码确实有效,但我想避免使用 _this(对我来说这是一种笨拙的方式),并开始使用 .call/.apply 或 .bind 来在以下示例中设置此上下文。 这是 j
我在下面的函数调用中收到一个错误,指出 this.albumDetails 对于特定行是未定义的,即使所有其他 this 调用都工作正常。 错误是:TypeError: _this.albumDeta
我是 React Native 的新手,请帮助我解决这个问题:TypeError: _props.goToScreen 不是一个函数,请帮助我提前致谢 Profile.js 我想单击 GotoHome
我试图通过转换现有应用程序来了解 ES6 和 Angular。毫不奇怪,挑战就是这个! let _$localStorage 来自 ( Injected dependencies not access
在我的 LoginProvider 中,我使用了一个函数来执行登录并将创建的 session 作为 promise 返回。 @Injectable() export class LoginProvid
我对工具提示进行了引导修改。 并使用 webpack/babel 处理我的 js 我的代码的简化可以是: $('[data-toggle="tooltip"]').tooltip({
尝试使用 angular2-flash-messages 并得到控制台错误 _this.flashMessagesService.show is not a function 按照 https://g
在下面的示例中我收到此错误 TypeError: _this is undefined 我尝试了很多方法,但找不到解决该错误的方法,我该如何解决? component_for_home_page.js
我是一名优秀的程序员,十分优秀!