StyleSheet.create“不属于流”

问题描述 投票:22回答:4

我使用同一个核素反应本地项目流程0.42.0。

使用默认的项目,我得到以下信息:

enter image description here

随着以下错误:

> flow check
index.ios.js:40
 40:             <View style={style.container}>
                                    ^^^^^^^^^ property `container`. Property cannot be accessed on possibly undefined value
 40:             <View style={style.container}>
                              ^^^^^ undefined

index.ios.js:40
 40:             <View style={style.container}>
                                    ^^^^^^^^^ property `container`. Property not found in
 40:             <View style={style.container}>
                              ^^^^^ Array

index.ios.js:41
 41:                 <Text style={style.welcome}>
                                        ^^^^^^^ property `welcome`. Property cannot be accessed on possibly undefined value
 41:                 <Text style={style.welcome}>
                                  ^^^^^ undefined

index.ios.js:41
 41:                 <Text style={style.welcome}>
                                        ^^^^^^^ property `welcome`. Property not found in
 41:                 <Text style={style.welcome}>
                                  ^^^^^ Array

index.ios.js:44
 44:                 <Text style={style.instructions}>
                                        ^^^^^^^^^^^^ property `instructions`. Property cannot be accessed on possibly undefined value
 44:                 <Text style={style.instructions}>
                                  ^^^^^ undefined

index.ios.js:44
 44:                 <Text style={style.instructions}>
                                        ^^^^^^^^^^^^ property `instructions`. Property not found in
 44:                 <Text style={style.instructions}>
                                  ^^^^^ Array

index.ios.js:47
 47:                 <Text style={style.instructions}>
                                        ^^^^^^^^^^^^ property `instructions`. Property cannot be accessed on possibly undefined value
 47:                 <Text style={style.instructions}>
                                  ^^^^^ undefined

index.ios.js:47
 47:                 <Text style={style.instructions}>
                                        ^^^^^^^^^^^^ property `instructions`. Property not found in
 47:                 <Text style={style.instructions}>
                                  ^^^^^ Array


Found 8 errors

这个问题(https://github.com/flowtype/flow-typed/issues/631)似乎表明,正确的类型是StyleSheet.Styles,但是这给了我同样的消息(和上面同样的错误):

enter image description here

有什么办法,我可以得到适当的流动打字在这里工作?

作为参考,完整的文件是:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

export default class PartalkReact extends Component {
    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                    Welcome to React Native!
                </Text>
                <Text style={styles.instructions}>
                    To get started, edit index.ios.js
                </Text>
                <Text style={styles.instructions}>
                    Press Cmd+R to reload,{'\n'}
                    Cmd+D or shake for dev menu
                </Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('PartalkReact', () => PartalkReact);

编辑升级到流动0.45.0,按照该意见,我不再有我的文件的问题,但反应本身失败,出现以下错误后:https://pastebin.com/raw/Ngpagayi

css react-native flowtype
4个回答
4
投票

该警告是因为第三方已经没有集成流向他们的结局。在我们的例子中,样式表给予警告等反应过来是本地人谁没有集成流量第三方。反应。在一些组件本土只包括流只可能是那些核心的,它被正式他们所申报。

至于说通过你的反应本地使用的样式表的流动,所以我得出一个结论:

import type { StyleObj } from 'react-
native/Libraries/StyleSheet/StyleSheetTypes';

type Props = {
    style?: StyleObj,
};

另一种方法是升级流程。

干杯:)


1
投票

我一直在使用以下到相当不错的成功 如果使用不存在的场所,启用代码完成这会显示错误

const rawStyles = { ... }
export const styles: typeof rawStyles = StyleSheet.create(rawStyles)

0
投票

对于任何人仍然在2018年后期遇到此,首选方案改为:

import type {
  ViewStyleProp,
  TextStyleProp,
  ImageStyleProp,
} from 'react-native/Libraries/StyleSheet/StyleSheet';

来源:https://github.com/flow-typed/flow-typed/issues/631


0
投票

增加这个类上面:

type Props = {
    style?: StyleSheet.Styles;
}; 
© www.soinside.com 2019 - 2024. All rights reserved.