【发布时间】:2019-10-15 15:31:48
【问题描述】:
我可以访问无状态组件中的函数吗?我知道如果更改为类组件,则可以访问该功能。然而,据我所知。 Hooks 不能与类组件兼容。请随意评论。谢谢。
# This example is incorrect, for describe the idea only.
const StatelessComponent = () => {
const StatelessComponentFunction = () => {
return 1;
}
}
const thisFun = new TestComponent();
let A = thisFun.StatelessComponentFunction();
2019 年 10 月 16 日更新,找到解决方案。
基于“https://hackernoon.com/javascript-state-encapsulation-without-classes-in-2019-97e06c6a9643”和“https://basarat.gitbooks.io/typescript/docs/javascript/closure.html”这些页面上的信息。我找到了解决办法。
修改
const StatelessComponent = () => {
return {
StatelessComponentFunction() {
return 1;
}
}
}
const thisFun = StatelessComponent();
console.log(thisFun.StatelessComponentFunction());
【问题讨论】:
标签: javascript typescript react-native