繁体   English   中英

React Native 项目中的意外令牌

[英]Unexpected Token in a React Native Project

我试图在调用 ES6 方法时使用这个Binding 上下文。 如何从称为回调的方法中访问对象? 作为从本书的 ES5 到 ES6 重构的指南; 但是,我似乎无法追踪我意外的令牌问题。 在构造函数完成后,这些文件都给了我一个问题。 任何帮助表示赞赏。

天气项目.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>
);
}
}

预测.js

 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 语法中,函数后不再需要逗号

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM