2017-06-12 80 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