考虑这个伪代码:
component.js
...
import {someFunc} from "./common_functions.js"
export default class MyComp extends Component {
constructor(props) {
super(props);
this.someFunc = someFunc.bind(this);
this.state = {...};
}
_anotherFunc = () = > {
....
this.someFunc();
}
render() {
...
}
}
common_functions.js
export function someFunc() {
if(this.state.whatever) {...}
this.setState{...}
}
我如何将函数绑定someFunc()
到 的上下文Component
?我在各种组件中使用它,因此将它们收集在一个文件中是有意义的。现在,我收到错误“无法读取任何未定义的内容”。上下文this
未知...