在TextInput中更改文本时,React-Native _this5.setState不是函数吗?

时间:2018-07-02 03:49:54

标签: react-native textinput

如果答案在其他地方出现,我目前正在学习React-Native道歉。但是在我的应用程序中,每当我更改TextInput中的文本时,程序就会崩溃,并显示错误_this5.setState is not a function

即如果我搜索“ Hello”,一切正常。但是当我将“ Hello”更改为“ Hell”时,程序因该错误而崩溃。

这是我的代码

export default class App extends React.Component {

  constructor(props){
    super(props)
    this.state = {
      apiKey: 'insertMyAPIKey',
      isLoading: true,
      text: ''
    }
    this.setInputState = this.setInputState.bind(this)
  }
  render() {
    return (
      <View style={{padding: 10}}>
        <TextInput
          style={{height: 40}}
          placeholder="Search For Summoner!"
          value={this.state.text}
          onChangeText={(text) => this.setState({
            text: text
          })}
          clearButtonMode="while-editing"
        />
        <Button 
          onPress={() => {
            this.MyFunctionsToRun(this.state.text)
          }}
          title="Search"
          color="#841584"
        />
        <Text style={{padding: 10, fontSize: 20}}>
          Searching for summoner: {this.state.text}
        </Text>
        <Text>
          The summoner ID: {this.state.summonerID}
        </Text>
        <Text>
          The summoner Rank: {this.state.leagueName}, {this.state.tier} {this.state.rank}, {this.state.leaguePoints}
        </Text>
        <Text>
          The Summoner Win Rate: {this.state.winRate}
        </Text>
        <View>
          <FlatList 
            data={this.state.lastTenGames}
            renderItem={({item}) => <Text>{item.lane}</Text>}
          />
        </View>
      </View>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试

onChangeText={text => (this.state.text = text)}

或使用其他功能

onChangeText={this.changeText}

changeText = text => {
    this.setState({text: text});
};

我希望它对您有用:D