javascript - 使用 redux 时出现 eslint 奇怪的错误

标签 javascript reactjs ecmascript-6 redux eslint

<分区>

我从 eslint 收到了这个警告,我正在使用 create-react-app。

./src/components/auth.js
  Line 24:  Unexpected labeled statement                                           no-labels
  Line 24:  'authenticated:' is defined but never used                             no-unused-labels
  Line 24:  Expected an assignment or function call and instead saw an expression  no-unused-expressions

而且我认为我下面的组件没有任何问题,这太烦人了

import React, { Component } from 'react';
import { connect } from 'react-redux';

export default function(ComposedComponent) {
  class Authentication extends Component {

    componentWillMount() {
      if (!this.props.authenticated) {
        this.props.history.replace('/login');
      }
    }

    componentWillUpdate(nextProps) {
      if (!nextProps.authenticated) {
        this.props.history.replace('/login');
      }
    }

    render() {
      return <ComposedComponent {...this.props} />
    }
  }

  return connect(state => {authenticated: state.auth.authenticated})(Authentication);
}

我不知道应该修复什么,第一次使用 eslint。

最佳答案

当你写这篇文章时,javascript 很困惑。

它看到一个返回指令 authenticated: state.auth.authenticated 的箭头函数,这是一条错误的指令。


你可以这样写:

connect(state => ({ 
     authenticated: state.auth.authenticated,
}));

我们添加括号来告诉javascript这不是指令而是json。

或者

connect((state) => { 
   return {
     authenticated: state.auth.authenticated,
   };
});

关于javascript - 使用 redux 时出现 eslint 奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47395086/

相关文章:

javascript - Babel 和 Browserify/Webpack 混淆

javascript - HTML5 FileReader 无法触发函数

javascript - React Navigation 5 - 在导航到另一个选项卡中的另一个堆栈之前重置堆栈(类似于 popToTop())

javascript - 如何将 CSS Mixins 应用于原生 Javascript?

javascript - React.unmountComponentAtNode | React.unmountComponentAtNode 15.4.1 |对象不支持属性或方法 'unmountComponentAtNode'

javascript - 在 React.js 中实现自己的本地化引擎 - 值得吗?

javascript - 函数提升在有和没有 block 范围的情况下显示不同的结果

javascript - 取消选中特定行中的复选框的语法是什么?

javascript - 为什么我无法获取输入值

javascript - 在 Firebase 的不同节点中写入多个对象