【问题标题】:"(AppContainer)RangeError: Maximum call stack size exceeded" - React with react hot loader“(AppContainer)RangeError:超过最大调用堆栈大小” - 与反应热加载器反应
【发布时间】:2019-07-04 09:17:30
【问题描述】:

我正在为非常奇怪的错误而苦苦挣扎,过去 2 小时我没有找到解决方案,所以我决定寻求帮助。

我已经使用 TypeScript 设置了 React、Redux、Webpack、React Hot Loader。

我使用了样板文件,但在遇到此问题后,我更改了 webpack 配置以反映 RHL 存储库中的示例。

它正在正确编译,但我无法使受保护的路由正常工作,因为如果用户经过身份验证,所以它应该呈现提供的组件,它会从这个问题的标题中抛出错误。

这是我的 ProtectedRoute 组件:

import React, { Component, FunctionComponent } from 'react';
import { Redirect, Route } from 'react-router';
import { isAuthenticated } from './auth';

interface IProps {
  component: Component | FunctionComponent;
  path: string;
}
const ProtectedRoute: FunctionComponent<IProps> = ({ component, ...rest }) => (
  <Route
    {...rest}
    render={(props) => {
      if (isAuthenticated()) {
        console.log('should render', component);
        return <Component />;
      }
      return <Redirect to="/login" />;
    }}
  />
);

export default ProtectedRoute;

就这么简单。

我正在尝试使用:

<ProtectedRoute path="/dashboard" component={() => <div>Test</div>} />

isAuthenticated 到目前为止是一个非常简单的函数:

export const isAuthenticated = () => {
  const accessToken = localStorage.getItem(ACCESS_TOKEN_STORAGE_KEY);
  console.log('checking auth');
  if (accessToken) {
    return true;
  }
  return false;
};

我可以将任何东西传递给受保护的路线,它总是会抛出:

(AppContainer)RangeError: 超出最大调用堆栈大小

这是调用堆栈:

react-hot-loader.development.js?2cd8:2202 Uncaught RangeError: 超出最大调用堆栈大小 在 renderReconciler (react-hot-loader.development.js?2cd8:2202) 在 Object.asyncReconciledRender [作为 componentWillRender] (react-hot-loader.development.js?2cd8:2220) 在 Component.hotComponentRender (react-hot-loader.development.js?2cd8:718) 在 Component.proxiedRender (react-hot-loader.development.js?2cd8:750) 在 Component.hotComponentRender (react-hot-loader.development.js?2cd8:730) 在 Component.proxiedRender (react-hot-loader.development.js?2cd8:750) 在 Component.hotComponentRender (react-hot-loader.development.js?2cd8:730) 在 Component.proxiedRender (react-hot-loader.development.js?2cd8:750) 在 Component.hotComponentRender (react-hot-loader.development.js?2cd8:730) 在 Component.proxiedRender (react-hot-loader.development.js?2cd8:750)

我真的不知道发生了什么。

我已尝试更改配置:

setConfig({
    logLevel: 'debug',
    ignoreSFC: true, // the same error
    pureRender: true // change error to instance.render is not a function
  });

但它没有帮助。

我将非常感谢任何帮助。

具有重复问题的回购:https://github.com/murbanowicz/rhl-issue

【问题讨论】:

  • 您的代码是否在某个地方,例如CodeSandbox?
  • 并非如此。很难把它放在某个地方,因为它是大型单声道回购等的一部分。我可以在这里发布所需的代码
  • 好的。恐怕很难在没有看到完整背景的情况下说出可能出了什么问题。如果您可以创建一个Minimal, Complete, and Verifiable example,那么提供帮助会容易得多。
  • 我已经添加了 repo。它并不是真的很小,但我不想删除所有东西,所以它可以在没有一些部门的情况下开始工作,但我无法让它与所有其他东西一起工作。 github.com/murbanowicz/rhl-issue
  • @Tholle 有什么想法吗?

标签: javascript reactjs webpack react-hot-loader


【解决方案1】:

ProtectedRoute 的渲染方法返回 React.Component 而不是 props 中传递给它的组件。

【讨论】:

  • 是的。这是一个错误。为了更加隐含,我将道具更改为ProtectedComponent
猜你喜欢
  • 2017-05-04
  • 2017-02-04
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-19
相关资源
最近更新 更多