不能运行本地做出反应例如官方

问题描述:

下面是我从React's Native website复制的代码应该呈现文本输入一些格式设置功能:我使用create-react-native-app不能运行本地做出反应例如官方

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); 

如果我运行

npm run flow 

它显示大量的错误:

我的问题是 - 我在这里做得不对,或作出反应的网站已经过时的代码?

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); 
+0

您可能需要根据流程语法进行更改,btw什么是您的项目根目录名称 –

+0

@JigarShah目录名称是pizza。 – valk

+0

如果没有Flow类型检查,为什么要运行Flow?为什么还要在复制的示例代码上运行类型检查器?只需跳过检查示例即可。这也不在说明中。如果你想学习基础知识,你应该坚持他们实际要求你做的步骤。 –

看来super(props)在这种情况下是不正确的。所以我改变了

export default class PizzaTranslator extends Component 

class PizzaTranslator extends Component 

然后把它称为从export default class Main和它的工作。

我猜测反应原生文档是没有流注释编写的,目的是为了让不熟悉流的人更容易阅读。