2016-09-03 13 views
0

ではありません。(ネイティブと反応)のNullはこのコードの一部は、ネイティブのトラブルレンダリングをしているリアクトあるオブジェクト

You input {this.state.zip}. 

イム初心者が、私は「学習ネイティブに反応」のチュートリアル次まだ彼ました本のコードが機能していません。

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

class WeatherProject extends Component { 
    // If you want to have a default zip code, you could add one here 
    getInitialState() { 
    return ({ 
     zip: '' 
    }); 
    } 
    // We'll pass this callback to the <TextInput> 
    _handleTextChange(event) { 
    // log statements are viewable in Xcode, 
    // or the Chrome debug tools 
    console.log(event.nativeEvent.text); 

    this.setState({ 
     zip: event.nativeEvent.text 
    }); 
    } 
    render() { 
    return (
     <View style={styles.container}> 
     <Text style={styles.welcome}> 
      You input {this.state.zip}. 
     </Text> 
     <TextInput 
      style={styles.input} 
      onSubmitEditing={this._handleTextChange}/> 
     </View> 
    ); 
    } 
} 

var styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    borderWidth: 2, 
    height: 40 
    } 
}); 

AppRegistry.registerComponent('WeatherProject',() => WeatherProject); 
[enter image description here][1] 
+0

は「苦労」と「動作しない」とはどういう意味-だろうか?エラーメッセージが表示されますか?メッセージを投稿してください。それに行番号が含まれている場合は、あなたの投稿にある行を教えてください。 –

答えて

0

通常、getInitialStateはReact.createClassとともに使用されます。クラスとしてのコンポーネントを定義するために、次のコードは、コンストラクタでなければならない:

getInitialState() { 
    return ({ 
     zip: '' 
    }); 
    } 

コンストラクタ:ES6クラス(もしComponentに延びるのではなくcreateClassを使用しているもの)で

constructor() { 
    super(); 
    this.state = { 
    zip: '' 
    } 
} 
3

、初期状態でありますコンストラクタにthis.state = {zip: ''}を設定します。

は、だから、

class WeatherProject extends Component { 
    constructor(props){ 
    super(props); 
    this.state = { 
     zip: "" 
    } 
    } 
} 
関連する問題