【发布时间】:2017-01-05 18:19:20
【问题描述】:
我不断看到使用 => 或 .bind(this) 的答案,但这些解决方案都不起作用。
import React, { Component } from 'react';
import { View, Text, TextInput, StyleSheet } from 'react-native';
export default class MyWeatherApp extends Component {
constructor(props) {
super(props);
this.state = {};
}
getInitialState() {
return {
zip: '',
forecast: null,
};
}
_handleTextChange(event) {
var zip = event.nativeEvent.text;
this.setState({zip: zip});
}
解决方案:
_handleTextChange = (event) => {
var zip = event.nativeEvent.text;
this.setState({zip: zip});
alert('click');
}
【问题讨论】:
-
您目前如何尝试绑定
this?从您的代码中不清楚。 -
我们能看到整个代码吗?
-
首先在 es6 中,反应中没有任何称为 getInitialState() 的东西。你必须这样做 this.state={zip: '', forecast: null}
-
我正在学习 udemy 课程。 React Native 变化如此之快,以至于课程甚至跟不上语法变化。
标签: reactjs react-native