我有以下嵌套类结构:
import React, {Component} from 'react';
import {TextField} from '@material-ui/core';
import './ProfileEditor.css';
export default class ProfileEditor extends Component {
SyncedTextField = class SyncedTextField extends Component {
onChange = event => {
console.log(this);
};
render() {
return (
<TextField
{...this.props}
onChange={this.onChange}/>
);
}
};
render() {
return (
<form className={"ProfileEditor"}>
<this.SyncedTextField/>
</form>
);
}
}
当代码被 Webpack 捆绑并在 Firefox 中运行时,它可以this.onChange
正确运行,但输出的this
是ProfileEditor
类的上下文。
这太奇怪了,因为在 JSX 中,当我提到“this”时,它正确地指向 SyncedTextField,但在 onChange 方法中,它指向ProfileEditor
.
我确实添加了一些属性来ProfileEditor
自己进行完整性检查,并且这些属性显示为 中声明的ProfileEditor
,即使在 SyncedTextField 中提供了一个冲突的定义。
有人可以告诉我如何避免这个问题,以及可能导致它的原因是什么?