我如何通过eslint检查this.props.navigation.navigate(react-navigation)?

Ded*_*zah 18 eslint react-proptypes react-navigation

我在我的反应原生项目中使用eslint airbnb.如果我没有验证道具,特别是来自道具,那么eslint throw错误linting react-navigation.

道具验证1 我如何使用PropTypes验证这一点?

我试图像这样验证它:

IntroScreen.propTypes = {
  navigation: PropTypes.shape({
    navigate: PropTypes.func,
  }),
};
Run Code Online (Sandbox Code Playgroud)

但仍然有这样的错误 错误lint 2

我如何通过默认道具,我应该?

Ded*_*zah 23

我通过设置导航和导航道具来修复它.

IntroScreen.propTypes = {
  navigation: PropTypes.shape({
    navigate: PropTypes.func.isRequired,
  }).isRequired,
};
Run Code Online (Sandbox Code Playgroud)

如果我们将道具设置为可选,则eslint强制我们提供要设置的默认道具.因此,如果我们将道具设置为必需的,则eslint不会再次警告您.


Mik*_*len 19

除了Dede答案之外,您的项目可能会添加(基本上)所有组件的导航.为导航添加道具验证是一项不必要的任务,然后你可能会为特定道具沉默.

您可以通过在eslint配置中添加以下内容来执行此操作:

"rules": {
     "react/prop-types": ["error", { "ignore": ["navigation"] }]
}
Run Code Online (Sandbox Code Playgroud)

请记住,在使用之前,您需要确保导航实际上已添加到组件中.