javascript React Native项目中的意外令牌

watbbzwu  于 4个月前  发布在  Java
关注(0)|答案(2)|浏览(30)

我试图使用这个Binding context when calling ES6 method. How to access object from within method called as callback?作为从书中的ES 5到ES6重构的指南;然而,我似乎无法跟踪我意想不到的令牌问题。这些文件都在构造函数完成后给了我一个问题。任何帮助都是感激的。
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

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

v64noz0r

v64noz0r1#

删除每个函数末尾的逗号。在ES6语法中,函数后面不再需要逗号

flvtvl50

flvtvl502#

无效的节点版本也可能是导致此问题的原因之一
https://github.com/facebook/react-native/issues/34768#issuecomment-1261364555

相关问题