无法运行React Native官方示例

时间:2017-09-26 12:54:27

标签: react-native flowtype create-react-native-app

这是我从React's Native website复制的代码,它应该使用一些格式化功能呈现文本输入:

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

export default class PizzaTranslator extends Component {
  constructor(props) {
    super(props);
    this.state = {text: ''};
  }

  render() {
    return (
      <View style={{padding: 10}}>
        <TextInput
          style={{height: 40}}
          placeholder="Type here to translate!"
          onChangeText={(text) => this.setState({text})}
        />
        <Text style={{padding: 10, fontSize: 42}}>
          {this.state.text.split(' ').map((word) => word && '').join(' ')}
        </Text>
      </View>
    );
  }
}

// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => PizzaTranslator);

我正在使用create-react-native-app

如果我跑

npm run flow

显示许多错误:

我的问题是 - 我在这里做错了什么,或者React网站上的代码已经过时了?

App.js:7
  7:   constructor(props) {
                   ^^^^^ parameter `props`. Missing annotation

App.js:9
  9:     this.state = {text: ''};
                      ^^^^^^^^^^ object literal. This type is incompatible with
  6: export default class PizzaTranslator extends Component {
                                                  ^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?

App.js:18
 18:           onChangeText={(text) => this.setState({text})}
                                       ^^^^^^^^^^^^^^^^^^^^^ call of method `setState`
 18:           onChangeText={(text) => this.setState({text})}
                                                     ^^^^^^ property `text` of object literal. Property cannot be assigned on possibly undefined value
  6: export default class PizzaTranslator extends Component {
                                                  ^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?

App.js:21
 21:           {this.state.text.split(' ').map((word) => word && '').join(' ')}
                           ^^^^ property `text`. Property cannot be accessed on possibly undefined value
 21:           {this.state.text.split(' ').map((word) => word && '').join(' ')}
                ^^^^^^^^^^ undefined. Did you forget to declare type parameter `State` of identifier `Component`?


Found 4 errors


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

export default class PizzaTranslator extends Component {
  constructor(props) {
    super(props);
    this.state = {text: ''};
  }

  render() {
    return (
      <View style={{padding: 10}}>
        <TextInput
          style={{height: 40}}
          placeholder="Type here to translate!"
          onChangeText={(text) => this.setState({text})}
        />
        <Text style={{padding: 10, fontSize: 42}}>
          {this.state.text.split(' ').map((word) => word && '').join(' ')}
        </Text>
      </View>
    );
  }
}

// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => PizzaTranslator);

2 个答案:

答案 0 :(得分:0)

我猜测反应本机文档是在没有流注释的情况下编写的,目的是让那些不熟悉流程的人更容易阅读。

答案 1 :(得分:0)

在此上下文中似乎super(props)不正确。所以我改变了

export default class PizzaTranslator extends Component 

class PizzaTranslator extends Component 

然后从export default class Main调用它并且它正在工作。