反应路由器错误无法读取未定义的属性“ forEach”

时间:2019-09-30 10:06:29

标签: reactjs react-router

从15版本更新到16版本时,我收到此错误:

  

无法读取PrivateRoute组件中未定义的属性“ forEach”。

Package JSON显示这些版本:

"react": "^16.10.1",
"react-dom": "^16.10.1",
"react-router": "^5.1.1",
"react-router-dom": "^5.1.1",
"react-router-redux": "*"

这里是要查看的组件代码:

我尝试安装eslint和eslint-loader,因为我发现这可能是问题所在,但没有运气

import React from "react";
import { Route, Redirect } from "react-router-dom";

const PrivateRoute = ({
    component: Component,
    Authenticated,
    AdminPage,
    Admin,
    ...props
}) => (
    <Route
        {...props}
        render={props => {
            if (AdminPage) {
                if (Authenticated && Admin) {
                    return <Component {...props} />;
                } else {
                    return (
                        <Redirect
                            to={{
                                pathname: "/",
                                state: { from: props.location }
                            }}
                        />
                    );
                }
            } else {
                if (Authenticated) {
                    return <Component {...props} />;
                } else {
                    return (
                        <Redirect
                            to={{
                                pathname: "/login",
                                state: { from: props.location }
                            }}
                        />
                    );
                }
            }
        }}
    />
);

export default PrivateRoute;


1 个答案:

答案 0 :(得分:1)

以下是一些可以尝试的东西:

  • 将react-scripts降级到3.0.1
  • 从devDependencies中删除eslint
  • 删除package-lock.json,删除node_module,运行npm install

如果使用yarn,也可以将其添加到package.json:

"resolutions": {
    "eslint-loader": "3.0.2"
}

更多信息,请访问: https://github.com/facebook/create-react-app/issues/7753 (错误报告)