【发布时间】:2016-03-19 19:49:41
【问题描述】:
我得到了以下用 typescript 编写的组件。 (来自 absolutetyped.org 的类型定义)。我将 onWheel 事件绑定到一个函数。但是当它被触发时this 是未定义的,那么我应该如何访问引用的元素this.div,如果我想要/需要更改状态应该怎么做呢?
import React = require('react');
interface fooProps {
src: string;
}
class foo extends React.Component<fooProps, {}>
{
private div: HTMLDivElement;
public onWheel(e: React.WheelEvent): void {
e.preventDefault();
e.stopPropagation();
//Do stuff with the div, but 'this' is undefined!!
this.div;
}
public render(): JSX.Element {
return (
<div ref={(ref) => this.div = ref} onWheel= { this.onWheel} >
<img src={ this.props.src } />
</div >)
}
}
【问题讨论】:
标签: javascript reactjs typescript