首页 > 解决方案 > 使用新的 React Native 应用程序安装 Flow

问题描述

我正在尝试使用 React Native 应用程序开始使用 Flow,但我总是收到一个错误,即 node_module 没有正确配置 Flow。

Ultimate 在 .flowconfig 我添加了一行来忽略node_module导演下的所有内容,但这给了我一个错误,它找不到'react native'

>     Spawned flow server (pid=70665)
>     Logs will go to /private/tmp/flow/zSUserszSaxxxzSWorkspacezSprojectzSAwesomeProject.log
>     Monitor logs will go to /private/tmp/flow/zSUserszSaxxxzSWorkspacezSprojectzSAwesomeProject.monitor_log
>     Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
> App.js:3:40
>     
>     Cannot resolve module react-native.
>     
>          1│ /* @flow */
>          2│ import React from 'react';
>          3│ import { StyleSheet, Text, View } from 'react-native';
>          4│
>          5│ export default class App extends React.Component {
>          6│   render() {
> 
> Error
> ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
> App.js:5:34
> 
> Cannot use property Component [1] with less than 1 type argument.
> 
>      App.js
>       2│ import React from 'react';
>       3│ import { StyleSheet, Text, View } from 'react-native';
>       4│
>       5│ export default class App extends React.Component {
>       6│   render() {

在新的 RN 应用程序中安装 Flow 的正确方法是什么

create-react-native-app AwesomeProject

cd AwesomeProject
npm start

标签: javascriptreact-nativeflowtype

解决方案


有几步...

  1. 使用流类型的存储库为您的 node_modules 包安装流库定义(“libdefs”)
  2. 在您的.flowconfig中,忽略一些更多的 react-native 包。我使用 Expo,所以你可能不需要所有这些:

就像是...

[ignore]    
      <PROJECT_ROOT>/node_modules/editions
      <PROJECT_ROOT>/node_modules/npm
      <PROJECT_ROOT>/vendor/bundle

      ; Ignore various node_modules for react-native
      <PROJECT_ROOT>/frontend/native/node_modules/react-native-gesture-handler/.*
      <PROJECT_ROOT>/frontend/native/node_modules/react-native/.*
      <PROJECT_ROOT>/frontend/native/node_modules/expo/.*
      <PROJECT_ROOT>/frontend/native/node_modules/react-navigation/.*
      <PROJECT_ROOT>/frontend/native/node_modules/xdl/.*
      <PROJECT_ROOT>/frontend/native/node_modules/reqwest/.*
      <PROJECT_ROOT>/frontend/native/node_modules/metro-bundler/.*
      <PROJECT_ROOT>/frontend/native/node_modules/metro/.*

有一些社区定义的 react native flow types 像这样浮动,但我发现它比它的价值和any所有组件都有更多错误,就像这样。缺少类型定义并没有伤害到我。


推荐阅读