首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用react-hot-loader时超过了React最大调用堆栈大小

使用react-hot-loader时超过了React最大调用堆栈大小
EN

Stack Overflow用户
提问于 2017-11-16 19:46:30
回答 1查看 368关注 0票数 0

在使用react-hot-loader时,我遇到了一个奇怪的问题。

只有这种情况会抛出Uncaught RangeError: Maximum call stack size exceeded at PatientEdit.__test__REACT_HOT_LOADER__

代码语言:javascript
复制
class PatientEdit extends React.Component {
    test = () => {
        return 123
    }
    constructor(props) {
        super(props)
    }
    static propTypes = {
    }
    render() {
        return (
            <div>{this.test()}</div>)
    }
}

但以下三点都是正确的

代码语言:javascript
复制
// A
class PatientEdit extends React.Component {
    test(){
        return 123
    }
    constructor(props) {
        super(props)
    }
    static propTypes = {}
    render() {
        return (
            <div>{this.test()}</div>)
    }
}
// B
class PatientEdit extends React.Component {
    test(){
        return 123
    }
    constructor(props) {
        super(props)
    }
    static propTypes = {}

    render() {
        return (
            <div>{this.test()}</div>)
    }
}
// C
class PatientEdit extends React.Component {
    test = () => {
        return 123
    }
    static propTypes = {}
    render() {
        return (
            <div>{this.test()}</div>)
    }
}

按照文档中的说明进行加载器配置:.babelrc如下,在文件输入前添加babel-polyfillreact-hot-loader/patch

代码语言:javascript
复制
    // .babelrc
    {
        "presets": [["env", {"modules": false}], "react", "stage-1"],
        "plugins": [
            "react-hot-loader/babel",
            "transform-decorators-legacy",
            "transform-flow-strip-types",
            "transform-object-assign",
            "transform-runtime",
            "typecheck",
            "react-css-modules"
        ]
    }

一开始,我对这种奇怪的行为感到非常震惊,并忽略了错误堆栈。现在是我深入了解react-hot-loader机制并欢迎详细解释的时候了。

EN

回答 1

Stack Overflow用户

发布于 2017-11-16 20:07:51

与其说是真正的推理,不如说是猜测。(我希望能从头到尾听到答案)

箭头函数自动将自身绑定到它的运行时上下文。(在这种情况下,PatientEdit类实例,分别组件)。

我猜问题是,类实例还没有被创建,因为构造函数还没有被执行。但需要绑定实例。这可能会导致类代码再次运行(期望找到它的构造函数)。这就是导致循环的原因,最终导致调用堆栈溢出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47328863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档