2017-06-12 78 views
2

我试图让我的头绕着这个错误整天。我正在尝试将Flow添加到我的React应用程序中。React + Flow + eslint

我必须声明如下组件:对于MyComponentProps

type MyComponentProps = { 
    prop1: string, 
    prop2: string 
}; 

// @flow 
class MyComponent extends Component { 
    static defaultProps = { 
    prop1: 'a', 
    prop2: 'b' 
    }; 
    handleOnClick: (value: string) => void; 
    props: MyComponentProps; 
} 

和类型现在,流量是报告没有错误。但是,当我尝试运行应用程序,我得到eslint错误:

error 'defaultProps' is not defined no-undef 
error 'handleOnClick' is not defined no-undef 
error 'props' is not defined   no-undef 

我使用babel-eslinteslint-plugin-flowtype。 我已将flowtype/define-flow-type规则添加到我的.eslintrc文件中。以下是我的.eslintrc文件的相关部分:

{ 
    "parser": "babel-eslint", 
    "plugins": [ 
    "flowtype", 
    "react" 
    ], 
    "extends": [ 
    "plugin:flowtype/recommended" 
    ], 
    "rules": { 
    "flowtype/define-flow-type": 1, 
    "flowtype/use-flow-type" : 1, 
    "no-undef": 2, 
    "react/jsx-no-undef": 2 
    }} 

我错过了什么?

+0

你是否在声明或某个引用它们的地方获得了ESLint错误? – btmills

+0

关于声明。 – Maggie

+0

你有什么版本的eslint,babel-eslint和eslint-plugin-flowtype? – btmills

回答

1

您是否使用import React from 'react'导入了React?

+0

是:)这是一个工作组件,我只是试图向其添加类型安全。 – Maggie

+0

道具道具:MyComponentProps;' - >这是其中一个投诉 它不知道它在哪里宣布。如果这是一个组件,那么你需要使用propTypes或contextTypes。 https://www.npmjs.com/package/prop-types – Demon

+0

https://stackoverflow.com/questions/38786973/how-to-set-component-default-props-on-react-component Check out这个帖子。请询问您是否有其他问题。 – Demon