2016-03-29 16 views
0

私はこのBinding context when calling ES6 method. How to access object from within method called as callback?を本のES5からES6までのリファクタリングのガイドとして使用しようとしています。しかし、予期せぬトークンの問題を追跡できないようです。これらのファイルは両方とも、コンストラクタ関数が終了した直後に私に問題を与えました。どんな助けもありがとうございます。Reactネイティブプロジェクトで予期しないトークン

WeatherProject.js

class WeatherProject extends Component { 
constructor(props) { 
super(props); 
this.state = { zip: '', 
forecast: null}; 
}, 

_handleTextChange(event){ 
let zip = event.nativeEvent.text; 
this.setState({zip: zip}); 
fetch('http://api.openweathermap.org/data/2.5/weather?q=' 
    + zip + '&units=imperial') 
    .then((response) => response.json()) 
    .then((responseJSON) => { 
    console.log(responseJSON); 
    this.setState({ 
     forecast: { 
     main: responseJSON.weather[0].main, 
     description: responseJSON.weather[0].description, 
     temp: responseJSON.main.temp 
     } 
    }) 
    }) 
    .catch((error) => { 
    console.warn(error); 
    }) 
}, 

render() { 
let content = null; 
if (this.state.forecast !== null) { 
    content = <Forecast 
       main={this.state.forecast.main} 
       description={this.state.forecast.description} 
       temp={this.state.forecast.temp}/>; 
} 

return (
    <View style={styles.container}> 
    <Image source={require('image!flowers')} 
      resizeMode= 'cover' 
      style={styles.backdrop}> 
    <View style={styles.overlay}> 
     <View style={styles.row}> 
     <Text style={styles.mainText}> 
      Current weather for 
     </Text> 
     <View style={styles.zipContainer}> 
     <TextInput 
      style={[styles.zipCode,styles.mainText]} 
      returnKeyType='go' 
      onSubmitEditing={this._handleTextChange} /> 
     </View> 
     </View> 
     {content} 
    </View> 
    </Image> 
    </View> 
); 
} 
} 

Forecast.js

+0

あなたが受け取っているエラーを投稿できますか? – David

答えて

2
class Forecast extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
    zip: '', 
    forecast: { 
    main: 'Clouds', 
    description: 'few clouds', 
    temp: 45.7 
} 
}; 
}, 
render() { 
return (
    <View> 
    <Text style={styles.bigText}> 
     {this.props.main} 
    </Text> 
    <Text style={styles.mainText}> 
     Current conditions: {this.props.description} 
    </Text> 
    <Text style={styles.mainText}> 
     {this.props.temp} F 
    </Text> 
    </View> 
); 
} 
} 

各機能の終了時にコンマを削除します。 ES6の構文では、関数の後にコンマは必要なくなりました

+0

私はそれが実際にそれを修正したと思う、ありがとう。 – jaysig

+0

それは私のためにそれを固定しました、ありがとう! – Andi

関連する問題