【问题标题】:I get Parsing error: ':' expected.eslint in this code but can't see where我在此代码中收到解析错误:':' expected.eslint 但看不到在哪里
【发布时间】:2020-12-15 16:08:03
【问题描述】:

我学习了 React,现在我完全陷入了这个荒谬的错误。

这是一张图片,在代码下方,我做错了什么?

import React from 'react';
import { useNavigate } from 'react-router-dom';
import AuthUserContext from './context';

const WithAuthorization = condition => Component => {
    class withAuthorization extends React.Component {
        constructor() {
            super();
            this.navigateAway = this.navigateAway.bind(this);
        }

        navigateAway() {
            const navigate = useNavigate();
            navigate('/');
        }

        render() {
            return (
                <AuthUserContext.Consumer>
                    {authUser => (condition(authUser) ? <Component {...this.props} /> : { this.navigateAway })};
                </AuthUserContext.Consumer>
            );
        }
    }

    return withAuthorization;
};

export default WithAuthorization;

更新

感谢@antoineso 的回答,我删除了 {} 大括号,然后它停止抱怨,但随后我收到了一个更奇怪的错误:

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    像这样删除大括号:

      {authUser => (condition(authUser) ? <Component {...this.props} /> :  this.navigateAway )};
    

    对于 React,你传递了一个对象,这就是它要求你在 this 和 navigateAway 之间添加 : 的原因

    【讨论】:

    • 谢谢,我试过了,得到了this 更奇怪的错误,我检查了 Eslint 文档,但在我的水平上,我使用 React.Component 没有意义
    • @Kid 我认为那是因为你使用了 Hooks useNavigate()
    • 我尝试在 navigateAway() 中只使用一个 console.log('test') 并且错误仍然存​​在
    • ok 最好重启我的环境
    • 看到这个StackOverFlow answer可能对你有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多