0

初めてreact-nativeでクラスを使っているのですが、なぜか状態を変更できません。それがAPIかどうかを確認しましたが、APIは正常に動作します。ブートストラップと他のすべては正常に機能しますが、this.setState({ name: response.data.name });何らかの理由で機能しません。私が間違っていることを誰かが知っていますか?

import React from "react";
import { StyleSheet, View, Text, AsyncStorage, Button } from "react-native";
import axios from "axios";

export default class Dashboard extends React.Component {
  constructor() {
    super();
    this.state = {
      token: "",
      response: [],
      supported: true,
      displayName: "",
      id: 0,
      name: "",
      phone: 0,
      website: ""
    };
    this._bootstrap();
  }

  _bootstrap = async () => {
    const token = await AsyncStorage.getItem("accessToken");
    this.setState({ token: token });
  };

  changeName = async function() {
    try {
      response = await axios.get("general/organization/own/default");
      this.setState({ name: response.data.name });
      return;
    } catch (error) {
      console.log(error);
      return;
    }
  };

  render() {
    return (
      <View style={styles.container}>
        <Text>name: {this.state.name}</Text>
        <Button title="change name" onPress={this.changeName} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center"
  }
});

エラー: this.setState は関数ではありません。(In 'this.setState({name: response.data.name})', 'this.setState' is undefined)

4

3 に答える 3